ios – 使用Alamofire和Multer上传文件

ios – 使用Alamofire和Multer上传文件,第1张

概述我正在尝试使用Alamofire将图像数据从iOS上传到带有Multer的Express服务器. req.file是未定义的,req.body的格式为{file:< bytes> }.没有错误消息,但文件未出现.这是我的代码: var bodyParser = require('body-parser')var multer = require('multer')app.use(bodyPa 我正在尝试使用Alamofire将图像数据从iOS上传到带有Multer的Express服务器. req.file是未定义的,req.body的格式为{file:< bytes> }.没有错误消息,但文件未出现.这是我的代码:

var bodyParser = require('body-parser')var multer = require('multer')app.use(bodyParser.Json())app.use(bodyParser.urlencoded({ extended: false }))app.post('/API/photos/upload',function(req,res) {    var upload = multer({ dest: 'public/images/content/'}).single('file')    upload(req,res,function(err) {        if (err) {            console.log("Error uploading file: " + err)            return        }        // req.file = req.body        console.log(req.body) // form fIElds        console.log(req.file) // form file    })    res.Json('yeah')})

在iOS上:

let url = fullURL("API/photos/upload")Alamofire.upload(.POST,url,multipartFormData: { multipartFormData in        if let image = image {            if let imageData = UIImageJPEGRepresentation(image,0.5) {                multipartFormData.appendBodyPart(data: imageData,name: "file")            }        }        },enCodingCompletion: { enCodingResult in            switch enCodingResult {            case .Success(let upload,_,_):                upload.responseJsON { response in                    switch response.result {                    case .Success:                        print("success")                    case .Failure(let error):                        print(error)                    }                }            case .Failure(let enCodingError):                print(enCodingError)            }    })

这让我困惑了好几个小时,非常感谢任何帮助!

UPDATE
一个HTML表单在Express端点上运行良好,因此它绝对是Alamofire发送请求的问题.我已经尝试了一些使用Alamofire上传的示例,但它们都发送了相同的错误请求.必须有一种方法可以与HTML表单进行相同的请求,但使用Alamofire.

另一个更新
我现在只是使用busboy-connect,它运行良好,并且具有更大的灵活性.

解决方法 我只是能够让这个工作.事实证明,您必须使用Alamofire指定filename和mimeType,以便multer在服务器端获取上传.因此,添加图像的代码应如下所示:

if let image = image {    if let imageData = UIImageJPEGRepresentation(image,0.5) {        multipartFormData.appendBodyPart(data: imageData,name: "file",filename: "filename.jpg",mimeType: "image/jpeg")    }}
总结

以上是内存溢出为你收集整理的ios – 使用Alamofire和Multer上传文件全部内容,希望文章能够帮你解决ios – 使用Alamofire和Multer上传文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1059290.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存