【问题标题】:Image doesn't display on browser after uploading with AFNetworking使用 AFNetworking 上传后图像不显示在浏览器上
【发布时间】:2017-08-25 07:07:43
【问题描述】:

我在上传 AFNetworking 图片时遇到了一个非常奇怪的问题。该文件正确上传,我可以看到它已上传到我的服务器上。当我下载文件并在 mac 上打开它时,图像显示正常。

但是,相同的图像不会显示在浏览器上。不知何故,该文件已损坏,无法仅在浏览器上运行。

这是图片的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];

【问题讨论】:

    标签: objective-c amazon-s3 afnetworking afnetworking-3


    【解决方案1】:

    您的链接的响应标头包含Content-Type: application/octet-stream - 这应该是Content-Type: image/png

    【讨论】:

    • 您好,感谢您的回复。不过,我认为这不是问题……因为此图像:ashore-testing.s3.amazonaws.com/uploaded/deliverable97/version1/… 也具有八位字节流内容类型,但显示在浏览器中。然而奇怪的是,当我尝试 curl -I 时,原始完整的图像内容长度为 7838,而损坏的内容长度为 4629。
    • 我在 Obj-C 中的 AFNetworking 代码似乎有问题。我尝试使用 PHP(使用 multipart/formdata)上传,但图像不会像那样被破坏
    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2012-08-19
    • 2014-03-18
    • 2013-07-28
    • 1970-01-01
    相关资源
    最近更新 更多