http - ios multipart image upload, uploaded file is corrupted -
i making multipart post request server , works fine, jpeg i'm uploading doesn't have file extension , can't opened (the file size same orginal). i've tried on tho different servers , same error occured, i'm assuming it's issue of app 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() (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've tried post using posttestserver.com this result
i having similar problem: upload jpeg using multipart/form-data
, , when re-download same file server can't opened preview.app
, nor can used create instance of uiimage
, because corrupted.
i opened both files side-by-side filemerge.app (included in xcode.app/contents/applications/) and, surprise, corrupted file had 1 empty line (crlf) @ top; otherwise, identical:
evidently, inserting \r\n
just before append contents of file upload request's body.
i changed upload code this:
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") // !!!! 3 crlf body.appenddata(filedata) body.appendstring("\r\n--\(boundary)--")
...to this:
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") // !!!! 2 crlf body.appenddata(filedata) body.appendstring("\r\n--\(boundary)--")
...and downloaded file valid jpeg, identical 1 uploaded.
so suggest open both versions of file, side side, , see what's changed.
(filemerge complain "files not ascii.", ignore , proceed.)
Comments
Post a Comment