3
我想通過ajax上傳一些文本內容,稍後會解析。這是我的Javascript代碼: Ajax上傳和ASP.NET
$("#fuFile").change(function() {
var fileInput = document.getElementById("fuFile");
var file = fileInput.files[0];
var formdata = new FormData();
formdata.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open("POST", 'testhandler.ashx', true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", file.name);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.send(formdata);
});
哪裏fuFile
是我的文件輸入,並且testhandler.ashx
是服務器處理程序獲取上傳的文件。 (實際上我用另一個處理程序來解析文件的內容。)
但是當我嘗試這樣做:
HttpFileCollection fc = context.Request.Files;
它返回任何文件。但出於某種原因在IE中工作。
但是,當我試圖讓輸入流:
StreamReader stream = new StreamReader(context.Request.InputStream);
string text = stream.ReadToEnd();
的text
變量成爲(HTTP頭)+文件內容。
------WebKitFormBoundaryx16Mw7tnG6JeflIB\r\nContent-Disposition: form-data;name=\"file\"; filename=\"teste export.CSV\"\r\nContent-Type: application/vnd.ms-excel(..file content..)
這是確定的,但我用這個插件:http://valums.com/ajax-upload/
和插件返回我只有文件的內容,在女巫我能得到通過的InputStream的內容,我沒有收到任何HTTP頭。
這是完美的,但我想做一個沒有使用插件的上傳腳本。只需上傳並解析,返回一些結果。簡單而快速
所以,我的問題是:如何獲取由Ajax XHR上傳的文件內容,僅在任何瀏覽器中獲取文件內容?
http://stackoverflow.com/questions/ 10475313/ajax-file-upload-with-xmlhttprequest是一個類似的問題。以下是xhr2的瀏覽器支持信息http://caniuse.com/xhr2 – 2012-07-17 16:58:03
是的,我的瀏覽器支持xhr2。而類似的問題是不是類似於我的問題... – 2012-07-17 17:13:22
你讀過這個http://stackoverflow.com/questions/7595049/why-cant-i-upload-files-asynchronously? – Vismari 2012-10-11 19:13:20