Ajax Web API多部分上载文件数据始终为空,FormData popuplated
我刚刚进入Web API,正在尝试使用以下方法,但provider.FileData似乎总是出现空计数=0,但provider.FormData似乎没有问题。我不明白为什么会这样Ajax Web API多部分上载文件数据始终为空,FormData popuplated,ajax,asp.net-web-api,Ajax,Asp.net Web Api,我刚刚进入Web API,正在尝试使用以下方法,但provider.FileData似乎总是出现空计数=0,但provider.FormData似乎没有问题。我不明白为什么会这样 public async Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipart
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
试着这样做:
uploader.bind('FileUploaded', function (up, file) {
$('#' + file.id + " b").html("100%").css({ 'color': '#47C449' });
$('#' + file.id).css({ 'background-color': '#F7FFF7' });
var data = new FormData();
data.append('file', file);
var uri = sf.getServiceRoot('mysite') + 'upload/PostFormData';
var xhr = new XMLHttpRequest();
xhr.open('POST', uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText); // handle response.
}
};
xhr.send(data);
});
谢谢你的帮助。虽然我能够调用该方法,但provider.fileData仍显示为count=0,这是因为我试图传递一个对象,而不是多个对象吗?不幸的是,此方法不起作用。关于可能出现的问题,你还有其他想法吗?
uploader.bind('FileUploaded', function (up, file) {
$('#' + file.id + " b").html("100%").css({ 'color': '#47C449' });
$('#' + file.id).css({ 'background-color': '#F7FFF7' });
var data = new FormData();
data.append('file', file);
var uri = sf.getServiceRoot('mysite') + 'upload/PostFormData';
var xhr = new XMLHttpRequest();
xhr.open('POST', uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText); // handle response.
}
};
xhr.send(data);
});