Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax Web API多部分上载文件数据始终为空,FormData popuplated_Ajax_Asp.net Web Api - Fatal编程技术网

Ajax Web API多部分上载文件数据始终为空,FormData popuplated

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

我刚刚进入Web API,正在尝试使用以下方法,但provider.FileData似乎总是出现空计数=0,但provider.FormData似乎没有问题。我不明白为什么会这样

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);
});