.NET Web方法文件上传

发布于 2024-10-02 01:16:13 字数 143 浏览 0 评论 0原文

可以使用 FileUpload Control 和 WebMethod 上传文件吗?

我想避免使用 UpdatePanel 和 ScriptManagers。

我该怎么做呢? Web 方法会是什么样的参数?有例子吗?

谢谢!

is possible to upload a file by using FileUpload Control and WebMethod?

I would like to avoid the UpdatePanel and ScriptManagers.

How can I do it? What kind of parameter the Web Method would be? Is there any example?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

野味少女 2024-10-09 01:16:13

我无法找到您使用 WebMethod 询问的解决方案,因此我提出了使用 HTTPHandler 或更广为人知的 ASPX 控件/页面的替代解决方案。

为了实现你想要的,我使用Valums File Upload,有很多替代方案,但这是我发现非常适合我的情况的一个。您可以在此处找到更多信息、文档并下载 javascript 代码:

http://valums.com/ajax-upload/

该代码还给出了如何在服务器端处理请求的示例,但是,它不包含.net中的代码示例,所以我找到了这个项目。

http://www.codeproject.com/KB/aspnet/AspNetHandlerAjaxUpload.aspx

它使用Valums File Upload并在服务器端使用.Net C#处理文件上传请求。

总而言之,以下是如何在客户端使用Valums 文件上传

  1. 确保引用CSSJavascript > 从Valum File Upload网站下载的文件。
  2. 使用此代码创建文件上传控件

    ; <无脚本>

    请启用 JavaScript 以使用文件上传器。

  3. 使用此 javascript 代码设置文件上传控件

    <前><代码>$(函数 () {
    var uploader = new qq.FileUploader({
    元素: document.getElementById('divFileUpload'),
    操作:'文件上传.ashx',
    onComplete:函数(id,文件名,responseJSON){
    if (responseJSON.Success) {
    警报(“成功”);
    }
    }
    });
    });

服务器端

  1. 创建 ASHX 文件来处理请求从客户端。
  2. 示例代码

    公共类 FileUpload : IHttpHandler
    {
        公共无效ProcessRequest(HttpContext上下文)
        {
            //这里保存文件
    
            //返回Json值给客户端
            context.Response.Write("{ \"成功\": true }");
        }
    }
    
  3. 非常重要,向客户端返回JSON类型。

有关如何处理客户端请求的更多详细信息,请参阅上面的 URL。

所有功劳均归于 Andrew ValumsValums 文件上传Syed BASHAR使用 Valums 文件上传的 .Net 服务器代码< /em>.

I could not find solution you asked using WebMethod so I come up with alternative solution which is using HTTPHandler or better known as ASPX control/page.

To achieve what you wanted, I use Valums File Upload, there are many alternative out there but this is the one that I found very suitable for my case. You can find more information, documentation and download the javascript code here:

http://valums.com/ajax-upload/

The code also give example of how to handle the request in server side however, it doesn't include code example in .net so I found this project.

http://www.codeproject.com/KB/aspnet/AspNetHandlerAjaxUpload.aspx

Which use Valums File Upload and handle the file upload request using .Net C# on server side.

To summarize, here is how you use the Valums File Upload on client side:

  1. Make sure reference the CSS and Javascript file downloaded from Valum File Upload website.
  2. Use this code for creating file upload control

    <div id="divFileUpload">
        <noscript>
            <p>
                Please enable JavaScript to use file uploader.</p>
        </noscript>
    </div>
    
  3. Use this javascript code to setup the file upload control

    $(function () {
        var uploader = new qq.FileUploader({
            element: document.getElementById('divFileUpload'),
            action: 'FileUpload.ashx',
            onComplete: function (id, fileName, responseJSON) {
                if (responseJSON.Success) {
                    alert("Success");
                }
            }
        });
    });
    

On the server side:

  1. Create ASHX file to handle request from the client side.
  2. Sample code

    public class FileUpload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //Save the file here
    
            //Return Json value to client
            context.Response.Write("{ \"Success\": true }");
        }
    }
    
  3. Very important, return JSON type to the client.

For more detail about how to handle the request from client side, please refer to the URL above.

All credits go to the Andrew Valums for the Valums File Upload and Syed BASHAR for the .Net server code using Valums File Upload.

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