我面临一些网络服务问题。我在Web服务中指定了文件上传代码。但是,当我尝试从cshtml页面调用Web服务(asmx)时,我得到的是{location:null}
响应消息。
这是我尝试调用Web服务的javascript。
function uploadFiles(event)
{
event.stopPropagation();
event.preventDefault();
// Create a formdata object and add the files
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append("UploadedFile", files[i]);
}
var ajaxRequest = $.ajax({
type: "POST",
url: "/admin/public/webservices/fileupload/fileUpload.asmx/UploadFile",
contentType: false,
processData: false,
data: data,
success: function (response, textStatus){
alert(JSON.stringify(response));
},
error: function(jqXHR, exception, errorThrown){
alert(jqXHR.status + " : " + errorThrown);
},
failure: function (r) {
alert(r.responseText);
}
});
}
这是我的网络服务。
[WebMethod]
public string UploadFile()
{
try
{
var httpPostedFile = HttpContext.Current.Request.Files["UploadedFile"];
if (httpPostedFile != null)
{
try
{
// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Files/Integration/ImportUser"), httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
return "File Uploaded Successfully!";
}
catch (Exception ex)
{
return "Error occurred. Error details: " + ex.Message;
}
}
else
{
return "No files selected.";
}
}
catch (Exception ex)
{
return ex.Message;
throw;
}
}
我也取消注释[System.Web.Script.Services.ScriptService]
。我无法弄清楚问题所在。感谢。