【问题标题】:file upload html5 asp.net web method文件上传html5 asp.net web方法
【发布时间】:2013-09-16 20:30:29
【问题描述】:

我正在尝试使用带有 type="file" 的 html5 输入控件将文件上传到服务器。 客户端javascript代码如下。

var fd = new FormData();
        fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
        var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);

        xhr.open("POST", "Default.aspx", true);
        xhr.send(fd);

这里

xhr.open("POST", "Default.aspx/Test", true);

如果我给出一个 web 方法(“Test”),控件不会访问服务器,就好像我删除了 web 方法并给出了普通的

xhr.open("POST", "Default.aspx", true);

控件正在点击 page_load 方法。

我遗漏了什么以使 web 方法没有受到打击。

问候, 拉克什

【问题讨论】:

  • 能否提供 Default.aspx.cs 有用的代码?

标签: c# javascript asp.net html


【解决方案1】:

我遗漏了什么以使 web 方法没有受到打击。

首先你必须在你的 Default.aspx.cs 页面上有一个静态方法

public static void Test()

用 WebMethod 属性装饰。

using System.Web.services;

[WebMethod]
public static void Test()

通过这种方式,webmethod 会被命中,但我不确定该方法应该具有什么样的参数,或者您是否可以使用 HttpContext.Current.Request.Files 访问发布的文件

你可以看看这个例子:

http://www.codeproject.com/Questions/374136/Call-Page-Method-From-Jquery-Ajax-Call

【讨论】:

  • 对不起,我在最初的帖子中没有给你完整的信息。我在文件后面的代码中有测试方法。看起来像这样[WebMethod] [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] public void Test() { // DO something here}
  • 使方法在网页上静态
猜你喜欢
  • 2020-07-10
  • 2020-05-19
  • 2014-02-23
  • 2020-12-22
  • 2014-05-15
  • 2012-12-30
  • 2016-11-14
  • 2019-01-24
  • 2013-11-07
相关资源
最近更新 更多