简体   繁体   English

如何在不使用处理程序的情况下使用ajax从客户端将文件发送到服务器端?

[英]How to send file to server side from client side using ajax without using handler?

I have sent file to handler page using ajax.but now i am trying to send file to webmethod using ajax.i cant able to get the file.i have put my sample code below please give me any idea.我已使用 ajax 将文件发送到处理程序页面。但现在我正在尝试使用 ajax 将文件发送到 webmethod。我无法获取文件。我已将我的示例代码放在下面,请给我任何想法。
This is my javacript code这是我的 javacript 代码

 <input type="file" id="MsoBill"   class="MsoReqdit upload">

    var Frmdata = new FormData();
    var Files = $("#MsoBill").get(0).files;
    Frmdata.append("test", Files[0])


  $.ajax({
        type: "POST",
        url: 'MyForm.aspx/testAjax',
        data: '{test:' + Frmdata + '}',
       contentType: "application/json; charset=utf-8",
       dataType: "json",
        processData:false,
        success: function (res) { debugger; alert(res); },
        failure: function (result) {
            alert("fail");
        }
    });

My server side code我的服务器端代码

[WebMethod]
public static int testAjax(Object test)
{

        return 1;
}

you should receive (or cast) the test object to a HttpPostedFileBase, then do您应该将测试对象接收(或转换)到 HttpPostedFileBase,然后执行

 MemoryStream target = new MemoryStream();
 test.InputStream.CopyTo(target);
 byte[] data = target.ToArray(); //here are the bytes

then simply save the file or do what you need:然后只需保存文件或执行您需要的操作:

System.IO.File.WriteAllBytes("path",bytes);

or

test.SaveAs("path");

As you need根据您的需要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JQUERY使用AJAX将Javascript变量(从客户端)发布到PHP文件(服务器端) - How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY 如何使用Ajax将客户端JavaScript变量发送到服务器 - How do you send a client side javascript variable to your server using ajax 如何使用 Ajax 从服务器端(nodejs)发送 JSON 到客户端? - How to send JSON from server-side (nodejs) to client-side with Ajax? 如何以 GET 方法将数据从客户端发送到服务器端 - ajax - how to send data in GET method from client side to server side - ajax 如何将 javascript 文件(其中包含多个函数)从服务器发送到客户端? - How to send a javascript file (with several functions in it) from the server to the client side? 首先调用服务器端代码,然后调用客户端脚本,而无需使用AJAX - first invoke server side code then client side script without using AJAX 使用 express 和 mongoose,如何使用 POST 路由将多个 ID 的数组从客户端发送到服务器端? - Using express and mongoose, how do I send an array of multiple IDs from the client-side to server-side using a POST route? 如何使用AJAX将字节数组发送到服务器端 - How to send byte array to server side using AJAX 不使用ajax从javascript调用服务器端函数 - calling a server side function from javascript without using ajax 在没有AJAX的情况下使用Javascript的服务器端方法 - Using Server side method from Javascript without AJAX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM