【发布时间】:2018-10-25 04:09:19
【问题描述】:
我必须使用多部分请求在服务器上上传文件。对于网络通话,我使用的是 Alamofire。
到目前为止我所做的如下
多部分请求:-
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
let fileData = Filedata() // getting data from local path
let URL = try! URLRequest(url: "https://SomeUrl/upload", method: .post, headers: headers)
Alamofire.upload(multipartFormData: { (multipartFormData) in
//multipartFormData.append(fileData, withName: "image", fileName: "image", mimeType: "image/png")
multipartFormData.append(fileData, withName: "file")
}, with: URL, encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
回应:-
{ Status Code: 400, Headers {
Connection = (
close
);
"Content-Type" = (
"application/json;charset=UTF-8"
);
Date = (
"Tue, 15 May 2018 10:34:15 GMT"
);
"Transfer-Encoding" = (
Identity
);
} }
[Data]: 171 bytes
[Result]: SUCCESS: {
error = "Bad Request";
message = "Required request part 'file' is not present";
path = "/files/safebolt.org/upload";
status = 400;
timestamp = "2018-05-15T10:34:15.715+0000";
}
谁能告诉我我在请求中做错了什么?
【问题讨论】:
-
你可以使用 alamofire
-
我提出的要求是用 alamofire。
-
你是上传单张图片还是多张图片
-
你试过
multipartFormData.append(fileData, withName: "file", fileName: "file", mimeType: "image/png")吗? -
@GreyHands 请在回答中发布上述行。我会接受它。它有效。
标签: ios iphone swift file-upload alamofire