gpt4 book ai didi

file-upload - 谷歌驱动器 + Alamofire : Uploading a file with properties

转载 作者:行者123 更新时间:2023-12-01 13:42:53 27 4
gpt4 key购买 nike

我正在尝试通过 Swift 2/Alamofire 将文件和参数上传到 Google 云端硬盘。在下面的代码中,我更改了以下行:

"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--

如果是这样,解决方法是什么?

最佳答案

仔细检查 Google 云端硬盘的 API 文档。

参数的关键字段似乎是“名称”(而不是“标题”)。

如果您想要额外的自定义文件属性,仅限于单个应用程序,请将“appProperties”添加到 json:

“应用程序属性”:{ “标题”:“随便”

关于file-upload - 谷歌驱动器 + Alamofire : Uploading a file with properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450245/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com