简体   繁体   中英

Swift: Content-Disposition appearing in image source when upload with alamofire

I am uploading image file on azure server with below code which successfully upload the file. (i can see it on server)

self.manager.upload(multipartFormData: { (multipart) in
        
        multipart.append(fileData, withName: "file",fileName: "uploading.png" , mimeType: "image/png")
        
    }, to: url.description, method: .put, headers: ["x-ms-blob-type":"BlockBlob","Content-type": "multipart/form-data","Content-Disposition" :"form-data"]) { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.response { answer in
                print("statusCode: \(String(describing: answer.response?.statusCode))")
                comp(true,"Documnet Uploaded Successfully")
            }
            
            upload.uploadProgress { progress in
                //call progress callback here if you need it
            }
        case .failure(let encodingError):
            print("multipart upload encodingError: \(encodingError)")
            comp(false,"Unable to Upload the documnet")
        }
}

But when i download the file (manually or by code). The image is not getting open. It say's

It may be damaged or use a file format that Preview doesn't recognise.

When i Open the file in NotePad. Below lines are appearing on top in every uploaded image. File is 'OKAY' without these lines. I don't know why these are added every time automatically.

--alamofire.boundary.5243a11a13e8a8f4

Content-Disposition: form-data; name="file"; filename="uploading.png"

Content-Type: image/png

Please suggest solution.

Not a direct answer, but here are some suggestions you can take a look.

1.Check if the image is fine before uploaded to azure.

2.When uploaded it to azure via code, check if you can view the correct image content in azure portal(In azure portal -> blob storage -> click the image -> then select Edit).

3.I think the root cause maybe due to incorrect request headers.

When uploaded the image to azure via code, please use tools like fiddler to check it's request / response headers.

Then manually uploaded an image to azure, also use fiddler to check it's request/response headers.

Then you can compare the headers(code vs manually), to find out what's the difference, then you can make some changes in your code accordingly.

Everything was working fine. Request, Headers each and everything. Problem was happening due to multipart request. It was a simple request to upload the Photo or Pdf to azure just like to upload it to FTP. It was not multipart request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM