[英]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.