【发布时间】:2014-05-21 10:45:48
【问题描述】:
我有一个 Web API 方法来上传文件并返回包含文件路径的 FileResult 对象。如何在 dropzone 中获取上传文件的文件路径(如何从 Web API 方法获取响应)?
我的拖放区:
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();
});
}
});
}
};
}
]);
我的 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
};
}
文件结果模型
public class FileResult
{
public IEnumerable<string> FilePaths { get; set; }
public string Submitter { get; set; }
}
谢谢!
【问题讨论】:
-
这个回调文件属性
myDropzone.on("complete", function (file) {有什么内容 -
其实就是文件名是文件名,不是FileResult类型(包含文件路径)
-
是的,你是对的。我刚刚检查了回调
myDropzone.on("complete", function (file)..并且file中有一个属性xhr包含来自API post 方法的响应。感谢您的帮助!
标签: javascript angularjs asp.net-web-api angularjs-directive dropzone.js