ASP.NET JavaScript Ajax 文件上传
我基本上希望将 HTML 页面和图像发布到 asp.net C# 页面,然后将该数据发布到服务器。
我发现了很多企业级和网络表单解决方案,但是还有其他方法可以避免网络表单吗?我不太喜欢生成 HTML。基本上,伪代码是这样的。
<script type="text/javascript">
window.onload = function() {
button.onclick = function() {
http = new XMLHttpRequest();
url = "imageuploader.aspx";
params = <dunno how to post file data. Help?>
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function () {
Test a bunch of stuff to see if image has been uploaded or we're still working on it
}
}
}
</script>
图片上传器.aspx
<%@ Page language="C#" validateRequest=false %>
<script language="C#" runat="server">
private void Page_Load (object sender, System.EventArgs e) {
<no clue what to do here since I've never done file uploading before>
}
<script>
I'm basically looking to post and image from an HTML page to an asp.net C# page and then have that data posted to the server.
I've found a lot of enterprise-y and webform-y solutions, but is there another way to do it avoiding webforms? I'm not a huge fan of generated HTML. Basically, the pseudocode would be like this.
<script type="text/javascript">
window.onload = function() {
button.onclick = function() {
http = new XMLHttpRequest();
url = "imageuploader.aspx";
params = <dunno how to post file data. Help?>
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function () {
Test a bunch of stuff to see if image has been uploaded or we're still working on it
}
}
}
</script>
imageuploader.aspx
<%@ Page language="C#" validateRequest=false %>
<script language="C#" runat="server">
private void Page_Load (object sender, System.EventArgs e) {
<no clue what to do here since I've never done file uploading before>
}
<script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,XMLHttpRequest不支持文件上传。您可以使用 jQuery 表单插件,表单插件使用隐藏的 iframe 元素来帮助完成任务。这是一种常见的技术,但它具有固有的局限性。 iframe 元素用作表单提交操作的目标,这意味着服务器响应将写入 iframe。
还有一些其他选项,
http://www.phpletter.com/Demo/AjaxFileUpload-演示/
http://pixelcone.com/jquery/ajax-file-upload-script/
现在,在服务器端,Request.Files 使您可以访问数组集合中的所有已发布文件。
First of all, XMLHttpRequest does not support file upload. You can use jQuery Form Plugin, the Form Plugin uses a hidden iframe element to help with the task. This is a common technique, but it has inherent limitations. The iframe element is used as the target of the form's submit operation which means that the server response is written to the iframe.
There are few other options as well,
http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
http://pixelcone.com/jquery/ajax-file-upload-script/
Now, on the server side, Request.Files gives you an access to all posted files in array collection.