[英]How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY
[英]How to send file to server side from client side using ajax without using handler?
我已使用 ajax 將文件發送到處理程序頁面。但現在我正在嘗試使用 ajax 將文件發送到 webmethod。我無法獲取文件。我已將我的示例代碼放在下面,請給我任何想法。
這是我的 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");
}
});
我的服務器端代碼
[WebMethod]
public static int testAjax(Object test)
{
return 1;
}
您應該將測試對象接收(或轉換)到 HttpPostedFileBase,然后執行
MemoryStream target = new MemoryStream();
test.InputStream.CopyTo(target);
byte[] data = target.ToArray(); //here are the bytes
然后只需保存文件或執行您需要的操作:
System.IO.File.WriteAllBytes("path",bytes);
或
test.SaveAs("path");
根據您的需要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.