Base64をファイルに戻す方法
- 選ぶ
- 貼る
- 保存する
APIの結果や、メールの中に、長い英数字の文字列(Base64)が入っていて、中身が画像やPDFらしい。ファイルとBase64を相互変換する の「Base64 → ファイル」で、元のファイルに戻せます。
手順
- 「向きを選ぶ」で、「Base64 → ファイル」を選びます。
- 「Base64の文字」に、文字列を貼り付けます。
- 「保存するファイル名」を入れます。拡張子もつけます(例:image.png、document.pdf)。
- 「ファイルとして保存」を押して、保存します。
- 保存したファイルを、開いて確認します。
拡張子の決め方
Base64の文字だけでは、元のファイルの種類が分かりません。次のような手がかりで、判断します。
- データURLの形式なら、先頭に「data:image/png;base64,」のように種類が書かれている
- 先頭の文字で、見当がつく場合がある(PNG画像は「iVBOR」、PDFは「JVBER」、JPEG画像は「/9j/」で始まることが多い)
- 取得元の説明(APIの仕様など)に書かれている
データURLの場合
「data:image/png;base64,」のような、先頭の部分がついている文字列は、そのまま貼り付けられます。または、先頭部分を除いた、コンマ以降の文字だけを貼り付けます。
保存後の確認
- 画像なら、ギャラリーや画像ビューアで開く
- PDFなら、PDFビューアで開く
- 開けない場合は、拡張子が違う可能性がある
向いている場面
- APIで受け取ったデータを、ファイルとして確認する
- メールの中の、埋め込みデータを取り出す
- 開発中の、テストデータを確認する
例
APIの応答に、Base64の長い文字列があった。「Base64 → ファイル」に貼り付け、ファイル名を「result.pdf」にして保存。PDFとして開けた。
つまずきやすい点
- 文字が途中で切れている:コピーが不完全。全体をコピーし直す。
- 改行やスペースが混ざっている:取り除いてから、貼り付ける。
- ファイルが壊れていると言われる:拡張子が違う、またはデータが欠けている。
よくある質問
長い文字列も貼り付けられますか?
はい。ただし、非常に長い場合は、時間がかかります。
まとめ
貼り付けて、拡張子つきのファイル名で保存。種類は、先頭の文字や説明から判断します。
ファイルとBase64を相互変換するを使う(無料)あわせて使えるツール
- ファイルのハッシュ値を調べる(SHA-256):ファイルのハッシュ値を調べて、照合します。
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルをZIPにまとめる:複数のファイルを、1つのZIPにまとめます。
同じツールの、ほかの解説
How to Turn Base64 Back into a File
- Choose
- Paste
- Save
An API result or an email contains a long string of letters and digits (Base64) that seems to be an image or PDF. "Base64 → file" in Convert Files and Base64 can turn it back into the original file.
Steps
- For "Direction", choose "Base64 → file".
- Paste the string into "Base64 text".
- Enter the "File name to save", with an extension (for example image.png, document.pdf).
- Press "Save as file" to save.
- Open the saved file and check it.
Deciding the extension
The Base64 text alone doesn't tell the original file type. Judge from clues like these.
- In data URL form, the type is written at the start, like "data:image/png;base64,"
- The first characters can give a hint (PNG images often start with "iVBOR", PDFs with "JVBER", JPEG images with "/9j/")
- It is stated in the source's documentation (such as the API specification)
For data URLs
A string with a leading part like "data:image/png;base64," can be pasted as it is. Or paste only the characters after the comma, excluding the leading part.
Checking after saving
- For an image, open in the gallery or an image viewer
- For a PDF, open in a PDF viewer
- If it won't open, the extension may be wrong
Good for
- Checking data received from an API as a file
- Extracting embedded data from an email
- Checking test data during development
Example
An API response contained a long Base64 string. Pasted it into "Base64 → file", named the file "result.pdf" and saved. It opened as a PDF.
Common stumbling blocks
- The text is cut off: the copy was incomplete. Copy the whole thing again.
- Line breaks or spaces are mixed in: remove them before pasting.
- Told the file is corrupted: the extension is wrong, or data is missing.
Frequently asked questions
Can long strings be pasted?
Yes. But very long ones take time.
Summary
Paste and save with a file name that has an extension. Judge the type from the first characters or documentation.
Use "Convert Files and Base64" (free)Tools that go well with this
- Check a File's Hash (SHA-256): Calculate a file's hash and compare it.
- Convert CSV and JSON: Convert between CSV and JSON instantly.
- Make a ZIP File: Pack several files into one ZIP.