使用HTML5在Asp.net拖放文件上传 [英] File upload using HTML5's drag and drop in Asp.net
问题描述
我试图上传使用HTML5的免打扰和文件API的文件。我不知道如何表单数据发送到服务器,我尝试使用XMLHtt prequest发送,但没有成功。这是我迄今。
Am trying to upload a file using HTML5's DnD and File API. Am not to sure how to send form data to the server, i tried to send using XMLHttpRequest but was not successful. This what i have so far.
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<br />
<div id="drop_area">Drop files here</div> <br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"/>
</form>
</body>
<script>
if (window.File && window.FileList && window.FileReader) {
var dropZone = document.getElementById('drop_area');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleDnDFileSelect, false);
}
else {
alert('Sorry! this browser does not support HTML5 File APIs.');
}
</script>
var files;
function handleDragOver(event) {
event.stopPropagation();
event.preventDefault();
var dropZone = document.getElementById('drop_zone');
dropZone.innerHTML = "Drop now";
}
function handleDnDFileSelect(event) {
event.stopPropagation();
event.preventDefault();
/* Read the list of all the selected files. */
files = event.dataTransfer.files;
/* Consolidate the output element. */
var form = document.getElementById('form1');
var data = new FormData(form);
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
var xhr = XMLHttpRequest();
xhr.open("POST", "Upload.aspx"); //Wrong ? not sure.
xhr.setRequestHeader("Content-type", "multipart/form-data");
xhr.send(data);
}
在我的MVC的操作方法:
In my MVC action method:
HttpFileCollection fileCollection = Request.Files;
for (int i = 0; i < fileCollection.Count; i++)
{
HttpPostedFile upload = fileCollection[i];
string filename ="c:\\Test\\" + upload.FileName;
upload.SaveAs(filename);
}
我知道我在UI按钮,截至目前我不使用。但是,正如你可以看到我尝试发送使用XMLHtt prequest的请求。任何人都可以建议我如何才能进一步学习。但我的服务器端code从未得到执行我不知道XMLHtt prequest是否成功。
I know i have a button in the UI, as of now am not using. But as you can see am trying to send a request using XMLHttpRequest. Can anyone suggest me how can i proceed further. But my server side code never get executed am not sure whether XMLHttpRequest was successful.
推荐答案
Jeezzz!其作品在IE罚款,我是在Chrome v 28,因为几天的测试。在IE文件被上传罚款。 (测试多文件上传)。所以,使其在Chrome中工作,我不得不做出一些改变。
*误区提出
Jeezzz!! Its works fine in IE, i was testing in Chrome v 28 since few days. In IE file gets uploaded fine. (tested multiple file uploads). So to make it work in Chrome i had to make few changes. * Mistakes made
-
在镀铬
虽然调试客户端,我发现
VAR XHR = XMLHtt prequest()抛出一个错误, DOM对象的构造不能作为函数调用
所以用它取代
In chrome While debugging client-side i found that var xhr = XMLHttpRequest() throws an error, "dom object constructor cannot be called as a function" So replaced it with
VAR XHR =新XMLHtt prequest();并删除 xhr.setRequestHeader(内容类型,多部分/表单数据); (不知道为什么,但xhr.send()导致的IsPostBack值是假的?)
var xhr=new XMLHttpRequest(); and removed xhr.setRequestHeader("Content-type", "multipart/form-data"); (Not sure why but xhr.send() results in ispostback value to be false?? )
注释是AP preciated。
链接到满code:<一href=\"http://programmingusingdotnet.blogspot.com/2013/08/aspnet-drag-and-drop-file-uploads-using.html\">http://programmingusingdotnet.blogspot.com/2013/08/aspnet-drag-and-drop-file-uploads-using.html
Comments are appreciated. Link to full code: http://programmingusingdotnet.blogspot.com/2013/08/aspnet-drag-and-drop-file-uploads-using.html
这篇关于使用HTML5在Asp.net拖放文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!