【发布时间】:2017-01-26 10:24:35
【问题描述】:
目前我正在尝试使用 Alamofire 上传图片,来自 Amazon 的示例实现如下:http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html
.NET 版本代码:
private void UploadS3FileByPresignedUrl(string url, string localFile, string contentType)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(localFile))
{
//// Need to add contentType otherwise the StreamContent is "binary/octet-stream"
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
var response = client.PutAsync(url, fileContent).Result;
response.EnsureSuccessStatusCode();
}
}
}
基本上我必须在两个条件下上传图片:
- 只支持jpg图片文件。
- 客户端获取上传URL后,需要使用内容类型为“image/jpeg”的HTTP PUT上传个人资料图片这里是.NET客户端的示例代码
我正在尝试通过使用 Alamofire 来做到这一点,但我没有成功...
我正在使用 swift 3 和 Alamofire 4,目前我有这个代码:
let image = UIImage(named: "Sky-Sunset")
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")},
usingThreshold:UInt64.init(),
to:".../my/profile/image/uploadurl",
method:.put,
headers:["Content-Type": "image/jpeg"],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
【问题讨论】:
标签: ios amazon-web-services swift3 alamofire