Reputation: 135
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!
Upvotes: 2
Views: 8819
Reputation: 319
Just add this snippet to your dropzone initializer
Dropzone.options.formUpload = {
init: function() {
this.on("success", function(data) {
var response = $.parseJSON(data.xhr.response);
});
}
}
Upvotes: 4