How to serialize a file in JavaScript?
I want to implement an asynchronous file upload for my asp.net application. My idea is to serialize the file into JSON client-side and call a web service to reconstitute the file server-side. My ques开发者_Go百科tion is:
How do a read a file as binary and serialize it into JSON? The solution can't require the use of Google Gears or "must-install" client-side technologies. Thanks!
Use an iframe to hide the file upload process. You don't want to process the data in JavaScript as it will be very slow. All you need to do is manage the user interaction.
Example: http://www.webtoolkit.info/ajax-file-upload.html
Since you can't access the file system with Javascript, I don't see how you would be able to do this, without some kind of Flash or ActiveX component installed.
It is not possible to read arbitrary files from the user's hard drive in Javascript, and it never will be.
If you don't understand why, imagine what would happen if any webpage could read %USERPROFILE%\ntuser.dat
.
If you want to upload a file using AJAX, you'll need to use an IFRAME
; see here.
You don't need to serialize a file to upload it and JSON is not involved in file uploads. Answers here may seem confusing because people think you are trying to upload files surreptitiously. See Will Bickford's answer above (iframes).
You need to ask how to accomplish the larger goal. You have a goal in mind and you've determined that the only way to accomplish that is to serialize a file with javascript on the client (which isnt possible) and some people have tried to decipher what you really want to accomplish and suggested answers (asynch uploads etc).
Tell us what you need to accomplish and describe the task and the limits of your solution and we can help you find a solution.
Some of the things we need to know
- why are you parsing the file?
- what file sizes do you expect to encounter?
- what are you doing with the information from the file?
- why are you doing it on the web instead of a desktop app?
精彩评论