简体   繁体   English

为什么要使用 ng-file-upload 上传服务

[英]Why use ng-file-upload Upload service

How come I have to use the Upload service with ng-file-upload like this为什么我必须像这样使用带有 ng-file-upload 的上传服务

Upload.upload({
    url: '/postMyFormHere',
    data:   {
        fileToUpload: model.file,
        someField1: model.field1,
        someField2: model.field2,
    }
})

and can't just simply do并且不能只是简单地做

  $http({
        method: 'post',
        headers: {
            'content-type': 'multipart/form-data'},
        url: '/postMyFormHere',
        data: {
            fileToUpload: model.file,
            someField1: model.field1,
            someField2: model.field2,
            }
    })

I thought ng-file-upload just enables binding of files from file inputs to the model.我认为 ng-file-upload 只是将文件从文件输入绑定到模型。 Can't Angular natively do a multipart/form-data POST request ? Angular 本身不能执行 multipart/form-data POST 请求吗?

Edit :编辑

I've been trying to make my multi-part post work via $http.我一直在尝试通过 $http 使我的多部分帖子工作。 I changed the header to :我将标题更改为:

"multipart/form-data; boundary=----WebKitFormBoundarypEvyJj8BAuq4lk7T"

But I get this error on my express server但是我在我的快递服务器上收到这个错误

Error: Multipart: Boundary not found

I'm assuming that the whole point of the Upload service is that it parses the POST payload with the multy part boundary right ?我假设上传服务的全部意义在于它使用多部分边界解析 POST 有效负载,对吗?

You absolutely can, the purpose of all modules which are created to handle uploading of files is browser support.您绝对可以,创建用于处理文件上传的所有模块的目的是浏览器支持。 IE10+ only support FormData() object. IE10+ 仅支持FormData()对象。 So, in case you need to support old browsers you need to solve this using <iframe> strategy, or just use existing libraries, which are basically doing the same.因此,如果您需要支持旧浏览器,您需要使用<iframe>策略来解决这个问题,或者只使用现有的库,它们基本上是一样的。

UPDATE更新

var fd = new FormData();
fd.append('file', model.file);
$http.post(uploadUrl, fd, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
})

Try this way!试试这个方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM