gpt4 book ai didi

ios - 在 Hapijs 后端处理 Alamofire 在 Swift 中发送的多部分/表单数据请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:27 25 4
gpt4 key购买 nike

我正在使用 swift 创建图像和视频上传 iOS 应用程序。我正在使用 Alamofire 库进行网络请求。下面是用户选择图片时快速运行的代码

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
print("Did not get required image")
return
}

guard let imageData = UIImageJPEGRepresentation(image, 0.8) else {
return
}

Alamofire.upload(.POST, "http://localhost:8101/upload_request", multipartFormData: {
multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "image")
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON(completionHandler: {
response in
debugPrint(response)
})
case .Failure(let encodingError):
print(encodingError)
}
})
}

下面是Hapijs处理请求的方式

server.route({
method: 'POST',
path: '/upload_request',
config: {
payload: {
maxBytes: 10 * 1024 * 1024,
output: 'stream',
parse: true,
allow: 'multipart/form-data'
}
},
handler: (request, reply) => {
var data = request.payload;
if (data.image) {
var filePath = 'new_image.jpg';
// data.image has data but is not of Stream.Readable

} else {
console.log('No data file');
}
}
});

问题是我没有在后端获取 request.payload.image 作为流,所以我无法创建 writeStream 并将其通过管道传输到 writeStream。我做错了什么,还是遗漏了什么?我应该怎么做才能读取数据并将其作为图像文件写入文件系统?

这是我在 request.payload.image 中获取的数据的开始

����JFIFHH��LExifMM*�i&��� ��8Photoshop 3.08BIM8BIM%��ُ���      ���B~��@ICC_PROFILE0ADBEmntrRGB XYZ �3;acspAPPLnone���-ADBE
cprt�2desc0kwtpt�bkpt�rTRC�gTRC�bTRC�rXYZ�gXYZbXYZtextCopyright 2000 Adobe Systems IncorporateddescAdobe RGB (1998)XYZ �Q�XYZ curv3curv3curv3XYZ �O��XYZ 4��,�XYZ &1/���� �"��

最佳答案

我找到了答案。只需要一个小的改变。我没有在 Hapi.js 端获得所需格式的数据的原因是我没有从 Alamofire 正确发送它。

这是从 Alamofire 发送请求的方式

Alamofire.upload(
.POST,
"http://localhost:8101/upload_request",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: self.fileData, name: "image", fileName: "imageToUpload.jpg",
mimeType: "image/jpeg")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON(completionHandler: { response in
debugPrint(response)
})
case .Failure(let encodingError):
print(encodingError)
}
})

以及它应该在后端处理的方式。只需确保您正在写入的目录已创建。

server.route({
path: '/upload_request',
method: 'POST',
config: {
payload: {
maxBytes: 20 * 1024 * 1024,
output: 'stream',
parse: true,
allow: 'multipart/form-data'
}
},
handler: (request, reply) => {
var data = request.payload;
if (data.image) {
var name = data.image.hapi.filename;
var path = __dirname + "/uploads/" + name;
var file = fs.createWriteStream(path);

file.on('error', (err) => {
console.error(err);
});

file.on('open', (err) => {
data.image.pipe(file);
});

data.image.on('end', (err) => {
var ret = {
filename: data.image.hapi.filename,
headers: data.image.hapi.headers
}
reply(ret);
})
}
}
});

关于ios - 在 Hapijs 后端处理 Alamofire 在 Swift 中发送的多部分/表单数据请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com