我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:
文件上传至谷歌时没有名称。否则,文件上传将失败,出现相同的通用错误代码:
我想知道 Alamofire 创建多部分请求的方式是否有问题,可能与 Google Drive 不兼容。根据 Google API 网站上的信息,请求需要具备一些参数,Alamofire 可能没有创建,例如内容长度和边界设置...
如果是这样,有什么解决方法?
我正在尝试通过Swift 2 / Alamofire将文件+参数上传到Google Drive。 在下面的代码中,如果我更改以下行:
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
对于以下内容:
"https://www.googleapis.com/upload/drive/v3/files"
文件上传至谷歌时没有名称。否则,文件上传将失败,出现相同的通用错误代码:
Error Domain=com.alamofire.error Code=-6003 "Response status code was unacceptable: 400" UserInfo={NSLocalizedFailureReason=Response status code was unacceptable: 400}
我希望能够上传文件并指定名称和其他可能的参数。我知道我在某种程度上破坏了多部分上传,但我不知道我做错了什么。
func postBinaryToGdriveSimple (token: String, callback: Bool -> Void){
var returnedId : String!
let path = NSBundle.mainBundle().pathForResource("drawing", ofType: "bin")
let bindata: NSData = NSData(contentsOfURL: NSURL(fileURLWithPath: path!))!
let parameters : [String: String] = ["title":"SomeFileName"]
let headers = ["Authorization": "Bearer \(token)"]
upload(
.POST,
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers: headers,
multipartFormData: { multipartFormData in
// append file parameters to request
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
// append binary file to request
multipartFormData.appendBodyPart(data: bindata, name: "upload", fileName: "drawing.bin", mimeType: "application/octet-stream")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
dispatch_async(dispatch_get_main_queue()) {
let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
//progress(percent: percent)
print ("................\(percent)")
}
}
upload.validate()
upload.responseJSON { response in
switch response.result {
case .Success(let data):
print(response)
print("Validation Successful")
let json = JSON(data)
returnedId = json[("id")].stringValue
print("......id for uploaded file is \(returnedId)")
callback(true)
case .Failure(let error):
print(error)
print("Validation Bad")
callback(false)
}
}
case .Failure(_):
callback(false)
}
})
} // end of postBinaryToGdriveSimple
我想知道 Alamofire 创建多部分请求的方式是否有问题,可能与 Google Drive 不兼容。根据 Google API 网站上的信息,请求需要具备一些参数,Alamofire 可能没有创建,例如内容长度和边界设置...
POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: number_of_bytes_in_entire_request_body
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"name": "My File"
}
--foo_bar_baz
Content-Type: image/jpeg
JPEG data
--foo_bar_baz--
如果是这样,有什么解决方法?