0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ランダムな文字列のファイル名を作成

Posted at

#概要
blobにランダムなファイル名をつけてアップロードしたい場合の処理をメモします。

#実装

.js
const imageName = Math.random().toString(32).substring(2)
const formData = new FormData()
formData.append('file', blob, imageName + '.jpg')
const config = {
    headers: {
      'content-type': 'multipart/form-data'
    }
}
await axios.post('/api/post_upload', formData, config).then(()=>{
      alert('画像アップロード完了')
})
  1. Math.randomで0以上1未満の少数をランダムで生成
  2. toString()で32進数に変換
  3. substring(2)で3文字目以降を取得

#まとめ
本格的にするならもっとセキュアな名前つけた方がいいかもしれません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?