我正在尝试从用Swift编写的OS X项目上传一个sqlite文件到Quickblox服务器。我已经在iOS上使用Quickblox iOS SDK解决了这个问题:
let file = QBCOFile()
file.name = "ThisFileName"
file.contentType = "sqlite"
let theData = NSData(contentsOfFile: originalPath)
file.data = theData
QBRequest.uploadFile(file, className: "MyClassName", objectID: cusObjID, fileFieldName: "FieldName", successBlock: {(response:QBResponse,fileUploadInfo:QBCOFileUploadInfo?) in
}, statusBlock: nil, errorBlock: {(response:QBResponse) in
showError()
})
使用OS X,我一直在使用Alamofire并获得了其他工作请求但不是这个。 API页面(https://quickblox.com/developers/Custom_Objects#Upload.2FUpdate_file)上的请求说明如下:
curl -X POST -H "QB-Token: e7cde5f7f5b93f3fa5ac72a281777cbf0f908374" -F "field_name=avatar" -F 'file=@"ava.jpg"' https://api.quickblox.com/data/<Class_name>/<record_id>/file.json
这个描述与其他描述之间的一个主要区别是&#34; -F&#34; s我不知道该怎么做。以下是我迄今为止尝试过的代码的一种变体:
Alamofire.upload(Method.POST, "https://api.quickblox.com/data/MyClassName/\(cusObjID)/file.json",
headers: ["QB-Token":realToken],
multipartFormData: { multipartFormData in
if let valData = "FieldName".dataUsingEncoding(NSUTF8StringEncoding) {
multipartFormData.appendBodyPart(data: valData, name: "field_name")
}
multipartFormData.appendBodyPart(data: theData, name: "ThisFileName", fileName: "ThisFileName.sqlite", mimeType: "multipart/form-data")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
我知道令牌和自定义对象ID(cusObjID)有效,因为我已在其他请求中使用它们。我已经在&#34; appendBodyPart&#34;上尝试了很多变化。部分,他们都给了我这个结果:
[Result]: SUCCESS: {
errors = (
"Wrong arguments"
);
}
并且响应已包含
Status = "404 Not Found";
我在StackOverflow上看到了一些答案,表明Alamofire可能无法用于此类上传,所以如果有其他方法可以实现这一点,那也很棒。真的,任何通过iOS Quickblox SDK完成iOS操作的方法都是我正在寻找的。 p>
更新
使用此问题的最后一个答案的编辑(Uploading file with parameters using Alamofire),我已经能够在文件上传时获得更新进度
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
Swift.print("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
}
但是我仍然遇到同样的错误。顺便说一句,这就是我的请求标题从成功的iOS版本看起来的样子:
Request headers: {
"Accept-Language" = "en-US;q=1";
"Content-Length" = 6488309;
"Content-Type" = "multipart/form-data; boundary=Boundary+88AB02F344BB7C4E";
"QB-Token" = b6f06b9e6e45c839aecc89019add6bd1;
"User-Agent" = "App Name/2.1 (iPhone; iOS 9.3; Scale/2.00)";
}
答案 0 :(得分:1)
do {
let opt = try HTTP.POST("https://api.quickblox.com/data/MyClassName/\(cusObjID)/file.json", parameters: ["field_name": "FieldName", "file": Upload(fileUrl: url)], headers: ["QB-Token":realToken])
opt.start { response in
if response.error == nil {
} else {
showError()
}
}
} catch {
showError()
}