admin管理员组

文章数量:1289391

Let's say I want to use some js encryption library (not java) to encrypt a file. Is it possible to do so on client side before sending the file to the server and so upload a file by javascript in some memory on client side ?

Could I use local storage for example at least for latest browsers ?

Let's say I want to use some js encryption library (not java) to encrypt a file. Is it possible to do so on client side before sending the file to the server and so upload a file by javascript in some memory on client side ?

Could I use local storage for example at least for latest browsers ?

Share Improve this question asked Jul 28, 2012 at 12:06 user310291user310291 38.2k86 gold badges294 silver badges518 bronze badges 1
  • yes you can...have a look at html5 file API! – Selvam Palanimalai Commented Jul 28, 2012 at 12:09
Add a ment  | 

2 Answers 2

Reset to default 6

You could use the File API

Here some examples: https://developer.mozilla/en/Using_files_from_web_applications)

Of course, as you imagined, you need latest browsers.

Yes you can. You have a few options to do that though.

Using FormData

const formData = new FormData()
formData.append('file', new Blob([fileTextContent], {type: 'text/plain'}))
await axios.post('/upload-file', formData)

Documentation: https://developer.mozilla/en-US/docs/Web/API/FormData/Using_FormData_Objects#creating_a_formdata_object_from_scratch

Set a file <input>'s value

let file = new File([data], "img.jpg",{type:"image/jpeg", lastModified:new Date().getTime()});
let container = new DataTransfer();
container.items.add(file);
fileInputElement.files = container.files;

Then you can submit the form containing the <input> normally.

The code above is from this SO answer: https://stackoverflow./a/66466855/14366961

本文标签: