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

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

问题描述

我正在尝试使用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)!)

在后端,我将其解码为:

and on the backend I am decoding it like:

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请求,如此处所述(其中上传的文件作为文件(例如PHP中的$_FILES),或者将二进制数据转换为文本(例如,使用base64),并且服务器代码已将image_data键的base64值转换回二进制数据.

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天全站免登陆