从Dropzone获取上传的文件

时间:2014-04-08 15:54:17

标签: javascript angularjs asp.net-web-api angularjs-directive dropzone.js

我有一个Web API方法来上传文件并返回包含文件路径的FileResult对象。如何在dropzone中获取上传文件的文件路径(如何从Web API方法获取响应)?

我的dropzone:

module.directive("dzDirective", [
    'apiAttachment', function(apiAttachment) {
        return {
            restrict: "A",
            link: function(scope, iElement, iAttrs, controller) {
                iElement.dropzone({
                    url:"/api/1/FileTransfer",
                    uploadMultiple: false,
                    init: function() {
                        var myDropzone = this;
                        myDropzone.on("complete", function (file) {
                            DoSomething();
                        });
                    }
                });
            }
        };
    }
]);

My Web Api方法:

[Route("api/1/FileTransfer")]
    public async Task<FileResult> Post()
    {
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
        }

        var storageFactory = new StorageFactory();
        var streamProvider = new StorageProvider(storageFactory.Create(storage, tempContainer));
        await Request.Content.ReadAsMultipartAsync(streamProvider);
        return new FileResult
        {
            FilePaths = streamProvider.Files
        };
    }

FileResult模型

public class FileResult
{
    public IEnumerable<string> FilePaths { get; set; }
    public string Submitter { get; set; }
}

谢谢!

1 个答案:

答案 0 :(得分:3)

只需将此代码段添加到dropzone初始化程序

即可
 Dropzone.options.formUpload = {
    init: function() {
        this.on("success", function(data) {
            var response = $.parseJSON(data.xhr.response);
        });
    }
}