如何在单个请求中使用精细上传器上传多个文件?

时间:2015-07-27 20:34:10

标签: file-upload asp.net-web-api

我想在单个请求中使用精细上传器上传多个文件。我正在使用web api 2作为端点。当我上传多个文件然后多个请求进入我的休息服务时,我现在发生了什么。精细上传器中是否有任何设置可以在多个文件上传时仅发送一个请求到休息服务。 我是一个很好的上传者,只是出于好奇,我想了解上传者如何与休息服务沟通,他们希望从休息服务中获得成功上传的响应。 任何帮助将不胜感激。

这是我的web api实现......

[Route("")]
    public Task<IEnumerable<FileDesc>> PostAsync()
    {


        if (Request.Content.IsMimeMultipartContent())
        {
            string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data"); //TODO: create "uploads" directory in the root for now.

            //MyStreamProvider streamProvider = new MyStreamProvider(uploadPath);
            var streamProvider = new CustomMultipartFormDataStreamProvider(uploadPath);
            // await Request.Content.ReadAsMultipartAsync(streamProvider);

            var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<IEnumerable<FileDesc>>(t =>
            {

                if (t.IsFaulted || t.IsCanceled)
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }

                var fileInfo = streamProvider.FileData.Select(i =>
                {
                    var info = new FileInfo(i.LocalFileName);
                    return new FileDesc(info.Name, info.Name, info.Length / 1024, true);
                });
                return fileInfo;
            });

            return task;

        }
        else
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
        }

    }

这是我的精细上传器初始化

serviceLevelViewModel.InitializeFineUploader = function (element) {
    currentElement = element;
    serviceLevelViewModel.manualUploader = new qq.FineUploader({
        element: element.find('.fine-uploader-manual-trigger')[0],
        template: 'qq-template-manual-trigger',
        request: {
            endpoint: settings.baseApiUri + 'DemandPackage',
            customHeaders: {
                "Authorization": 'Bearer ' + accessToken
            }                                
        },
        showMessage: function (message) {
            return utilities.ShowAlert(message);
        },
        validation: {
            acceptFiles: settings.acceptFiles,
            allowedExtensions: settings.allowedExtensions,
            sizeLimit: settings.sizeLimit,
            stopOnFirstInvalidFile: false
        },
        callbacks: {
            onError: function (id, name, errorReason, xhrOrXdr) {
                if (currentElement.find('.qq-upload-retry-selector').attr("class").indexOf("qq-hide") <= -1 || errorReason == 'XHR returned response code 0') {
                    element.find('.CancelAll').attr("class", "CancelAll");
                    element.find('.RetryAll').attr("class", "RetryAll");
                }
            },

            onValidateBatch: function (fileOrBlobDataArray) {

                var arrayFileList = [];
                var allowedExtensions = settings.allowedExtensions;

                var existingFile = element.find('.qq-upload-file-selector');

                for (var index = 0 ; index < existingFile.length; index++) {
                    arrayFileList.push(existingFile[index].title);
                }
                for (var index = 0 ; index < fileOrBlobDataArray.length; index++) {
                    arrayFileList.push(fileOrBlobDataArray[index].name);
                }
                if (arrayFileList.length > 0) {

                    for (var index = 0; index < arrayFileList.length; index++) {

                        if (allowedExtensions != "") {
                            allowedExtensions = arrayFileList[0].substring(arrayFileList[0].lastIndexOf('.') + 1, arrayFileList[0].length)
                        }
                        var extension = arrayFileList[index].substring(arrayFileList[index].lastIndexOf('.') + 1, arrayFileList[index].length)
                        if (allowedExtensions.substring(0,3).toLowerCase() != extension.substring(0,3).toLowerCase()) {
                            utilities.ShowAlert("Only one file type extension can be uploaded at one time.");
                            return false;
                        }
                    }
                }

            },
            onSubmitted: function (fileId, fileName) {
                var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
                serviceLevelViewModel.SetDocumentIcon(element, fileExtension);
            }

        },
        autoUpload: false,
        debug: false
    })

0 个答案:

没有答案