ASP.NET JavaScript Ajax 文件上传

发布于 2024-12-08 12:00:55 字数 1014 浏览 0 评论 0原文

我基本上希望将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-12-15 12:00:55

首先,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文