Ios multi-page image upload, uploaded file is corrupted

I am making a multipart POST request to the server and everything is working fine, but the jpeg I am uploading has no file extension and cannot be opened (file size is the same as original). I tried this on different servers and the same error happened, so I guess this is a problem in my application code.

let boundary = generateBoundaryString()
    let request = NSMutableURLRequest(URL: Urls.sendFileURL)
    request.HTTPMethod = "POST"
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    let body = NSMutableData()

    for (key, value) in params {
        body.appendString("--\(boundary)\r\n")
        body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
        body.appendString("\(value)\r\n")
    }


    let imageData: NSData = UIImageJPEGRepresentation(photo, 0.8)

    body.appendString("--\(boundary)\r\n")
    body.appendString("Content-Disposition: form-data; name=\"iosUpload\"; filename=\"iosUpload.jpg\"\r\n\r\n")
    body.appendString("Content-Type: image/jpeg\r\n\r\n")
    body.appendData(imageData)
    body.appendString("\r\n")
    body.appendString("--\(boundary)\r\n")

    request.HTTPBody = body

      

I tried my POST with posttestserver.com   this is the result

+3


source to share


2 answers


I had a similar problem, I download a jpeg with multipart/form-data

, and when I re-download the same file from the server, it cannot be opened Preview.app

and also cannot be used to instantiate UIImage

it because it is corrupted.

enter image description here

I opened both files side-by-side with FileMerge.app (included in Xcode.app/Contents/Applications/), and to my surprise, the damaged file had one extra blank line (CRLF) at the top; , they are identical :

Obviously I'm adding an extra \r\n

just before adding the content of my file to the body of the upload request.

I changed the download code:

let body = NSMutableData()

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"inputFile\"; filename=\"\(fileName)\"\r\n")
body.appendString("Content-Type: image/jpeg\r\n\r\n\r\n") // !!!! THREE CRLF
body.appendData(fileData)    
body.appendString("\r\n--\(boundary)--")

      



... to that:

let body = NSMutableData()

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"inputFile\"; filename=\"\(fileName)\"\r\n")
body.appendString("Content-Type: image/jpeg\r\n\r\n") // !!!! TWO CRLF
body.appendData(fileData)    
body.appendString("\r\n--\(boundary)--")

      

... and now the downloaded file is a valid jpeg identical to the one I downloaded.

So I suggest you open both versions of your file side by side and see what has changed.

(FileMerge will complain that "The files are not ascii.", But just ignore it and continue.)

+1


source


I'm not familiar with ios, but you can't find a library to build a multi-page query and have to roll your own?

I can at least solve the following problems:

1)

body.appendString("Content-Disposition: form-data; name=\"iosUpload\"; filename=\"iosUpload.jpg\"\r\n\r\n")

      

there is an additional one here \r\n

. it should be



body.appendString("Content-Disposition: form-data; name=\"iosUpload\"; filename=\"iosUpload.jpg\"\r\n")

      

2)

body.appendString("--\(boundary)\r\n")

      

there is no ending --

here. it should be

body.appendString("--\(boundary)--\r\n")

      

0


source







All Articles