繁体   English   中英

Alamofire 到 Node.js 的请求在服务器端没有被正确接收?

[英]Alamofire to Node.js request not being received properly on the server side?

我有一个 heroku 应用程序设置和一个 iOS 客户端。 iOS 客户端应该发送包含一些字符串数据和图像的发布请求。 在服务器端,我想将图像发布到 S3,但我不确定如何处理我的 request.file object。

我在下面设置了一个 Alamofire 请求:

func postRequest(selectedImage: UIImage) {
    let server = 'https://server.com'
    let title = 'test title name'
    let subtitle = 'test subtitle name'
            
    var parameters = [String: AnyObject]()
    parameters = ["title":title,
                  "subtitle":subtitle
        ] as [String: AnyObject]
    
    AF.upload(multipartFormData: { multiPart in
            for (key, value) in parameters {
                if let temp = value as? String {
                    multiPart.append(temp.data(using: .utf8)!, withName: key)
                }
                if let temp = value as? Int {
                    multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
                }
                if let temp = value as? NSArray {
                    temp.forEach({ element in
                        let keyObj = key + "[]"
                        if let string = element as? String {
                            multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                        } else
                            if let num = element as? Int {
                                let value = "\(num)"
                                multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                        }
                    })
                }
            }
        if let imageData = selectedImage.jpegData(compressionQuality: 0.9) {
            multiPart.append(imageData, withName: "cameraPhoto", fileName: "picName", mimeType: "image/jpg")
        }
        }, to: server)
            .uploadProgress(queue: .main, closure: { progress in
                //Current upload progress of file
                print("Upload Progress: \(progress.fractionCompleted)")
            })
            .responseJSON(completionHandler: { data in
                //Do what ever you want to do with response
                print("received the response")
            })
}

在服务器端,我只是使用 express 来处理所有传入的请求:

app.post('/', async function (request, response) {
  console.log(request.body) // this is all the non-image parts of the request
  console.log(request.file) // this is the image
  // in here, I want to take the image data and put the image in an S3 bucket, what AWS library command do I run to do this? Is any image data manipulation necessary?
  response.send("Hit the post")
});

我不确定如何在 Node.js 端处理多格式数据并将其发送到 S3 存储桶。 示例 request.file 如下:

{
    fieldname: 'cameraPhoto',
    originalname: 'picName',
    encoding: '7bit',
    mimetype: 'image/jpg',
    buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 00 58 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01 00 01 ... 1204620 more bytes>,
    size: 1204670
}

任何帮助深表感谢!

对于错误消息:

Multer: field value too long

代码:

app.post('/', upload.single('cameraPhoto'), async function (request, response, next) {
  // ...
  // ...
}

您可以在 Github Multer 查看问题 436 ,它描述了如何设置上传文件的默认限制大小。

对上述问题目的的最佳响应增加限制使用:

multer({
  limits: { fieldSize: 1_000_000 }
})

值以bytes为单位,默认值为1MB

可以在 express 文档、 express 中间件 multer中查看其他资源

暂无
暂无

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

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