我有一个上传功能,可以使用POST方法将照片发送到我的服务器。我现在还希望在此服务中包含变量userID(currentUser)。我有以下代码,但是当我将let param = ["userId" : "1"]
切换为类似let param = [currentUser]
的内容时,我收到了错误消息。 currentUser被声明为一个String,这是函数所要求的,所以我无法弄清楚什么是错的。
func myImageUpLoadRequest(){
let myUrl = NSURL(string: "http://server.com/POSTTEST/imageUpload.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let param = ["userId" : "1"]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(myImageView.image, 1)
if(imageData==nil) { return; }
request.HTTPBody = createBodyWithParameters(param, filePathKey: "imagefile", imageDataKey: imageData, boundary: boundary)