Get uploaded file from Dropzone

独自空忆成欢 提交于 2019-12-21 22:14:52

问题


I have a Web API method to upload file and return FileResult object that contains file path. How can I get the file path of uploaded file in dropzone (how to get response from Web API method)?

My 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 method:

[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 Model

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

Thanks!


回答1:


Just add this snippet to your dropzone initializer

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


来源:https://stackoverflow.com/questions/22942106/get-uploaded-file-from-dropzone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!