使用AFNetworking上传图片后不显示在浏览器中
问题描述:
AFNetworking Image上传时出现了一个非常奇怪的问题。文件上传正确,我可以看到它已经上传到我的服务器上。当我下载文件并在Mac上打开它时,图像显示正常。使用AFNetworking上传图片后不显示在浏览器中
但是,相同的图像不会显示在浏览器上。不知何故,该文件已损坏,无法在浏览器上运行。
下面是图像的link。如果您尝试在任何浏览器中打开,它将显示空白屏幕。
任何想法为什么会发生这种情况?我今天花了几个小时才弄清楚,但无济于事。我已经尝试上传PDF,它工作正常。
这里是我的代码:
NSDictionary *params = @{@"deliverable_ID" : deliverableID,
@"name" : @"Artboard.png",
@"type" : @"png",
@"description" : @"Some random png file"};
NSString *path = [[NSBundle mainBundle] pathForResource:@"Artboard" ofType:@"png"];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"Artboard.png" mimeType:@"image/png" error:nil];
} error:nil];
[request setValue:apiToken forHTTPHeaderField:@"Authorization"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:nil
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSString * result = [NSString stringWithFormat:@"Error: %@", error];
[delegate printResult:result];
} else {
NSString * result = [NSString stringWithFormat:@"%@", responseObject];
[delegate printResult:result];
}
}];
[uploadTask resume];
答
的响应头为你的链接包含Content-Type: application/octet-stream
- 这应该是Content-Type: image/png
代替。
嘿,谢谢你的回复。我不认为这是问题,虽然...因为这个图像:https://ashore-testing.s3.amazonaws.com/uploaded/deliverable97/version1/Artboard.png也有八位字节流内容类型,但显示在浏览器中。然而,当我尝试卷曲时,奇怪的是 - 我的原始未破碎图像内容长度是7838,而破坏的图像内容长度是4629. –
似乎在Obj-C中我的AFNetworking代码有问题。我尝试使用PHP上传(使用multipart/formdata),但图像不会像这样被破坏 –