使用多个参数快速上传图像 [英] uploading image in swift with multiple parameters

查看:20
本文介绍了使用多个参数快速上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 swift 将图像上传到后端客户端.问题是我似乎无法为 httpbody 获取正确的格式.我不想使用多部分表单进行上传,因为我不知道如何在后端处理它.

I am trying to upload an image to a backend client using swift. Trouble is I can't seem to get the formatting correct for the httpbody. I do not want to use a multipart form for uploading as I don't know how to handle that on the backend.

这是我的代码..当我在线查看图像时它不起作用,它不显示,它只有70kb,我知道这绝对不是图像有多大.

Here is the code I have.. it doesn't work when I view the image online it doesn't display and it is only like 70kb which I know is definitely not how big the image is.

var bodyString: String = "session_id=(session_id)&location_id=(location_id)"
bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
var body = NSMutableData.alloc()
body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
if image != nil{
    var imageData = UIImageJPEGRepresentation(image,0.5)
    body = NSMutableData.alloc()
    //var imageDataString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    bodyString = "session_id=(session_id)&location_id=(location_id)&image_data="
    bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
    var imageString = "&image_data=(imageData)"
    body.appendData(imageData)
    }
req.HTTPBody = body

更新:所以我决定采用 base64 路线,但它似乎仍然无法正常工作,因为我将它编码为 ntf8string,这是正确的做法吗?

UPDATE: so I decided to go the base64 route but it still doesn't seem to be working I think because I am encoding it as an ntf8string is this the correct way to be doing this?

var imageData = UIImageJPEGRepresentation(image,0.5)
var imageDataString = imageData.base64EncodedStringWithOptions(.allZeros)
body = NSMutableData.alloc()
bodyString = "session_id=(session_id)&location_id=(location_id)&tag_type=(tag_type)&image_data=(imageDataString)"
bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)

在后端我正在解码它:

image_data_decoded = base64.b64decode(image_data)

推荐答案

你不能像这样在 application/x-www-form-urlencoded 请求中发布二进制数据.实际上,您问题中的代码看起来会尝试发送二进制数据的十六进制字符串表示,这可能不是您想要的,即使您确实打算这样做,(a)您也必须对其进行解码不知何故在服务器端;(b) 请注意,这是非常低效的(图像有效负载的大小增加了一倍多):并且 (c) 需要在请求中进行百分比转义.但无论如何,我认为你根本不是故意的,所以这可能完全没有实际意义.

You cannot post binary data in a application/x-www-form-urlencoded request like this. Actually, the code in your question looks like it will try to send a hexadecimal string representation of the binary data, which, probably is not what you intended and even if you did intend to do that, (a) you would have to decode it somehow on the server side; (b) note that this is very inefficient (more than doubles the size of the image payload): and (c) would need to be percent escaped in the request. But I don't think you intended that at all, anyway, so that is probably all moot.

通常会创建 multipart/form-data 请求,如 here 所述(在上传的文件以文件的形式出现,例如 PHP 中的 $_FILES),或者将此二进制数据转换为文本(例如使用 base64)并且服务器代码已将 base64 值转换为 image_data键回二进制数据.

One would generally either create multipart/form-data request as outlined here (in which the uploaded file comes in as a file, e.g. $_FILES in PHP), or one would convert this binary data to text (e.g. using base64) and the the server code has convert the base64 value for image_data key back to binary data.

顺便说一句,我可能会建议 Alamofire 或 AFNetworking 作为尝试正确创建请求的替代方法.它不会改变根本问题(您必须在 base64 编码或多部分请求之间进行选择),但它简化了 Swift 代码.

By the way, I might suggest Alamofire or AFNetworking as alternatives to trying to create requests properly. It doesn't change the underlying issue (you have to pick between a base64 encoding or multipart requests), but it simplifies the Swift code.

这篇关于使用多个参数快速上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆