无法在Swift上使用多部分请求将图像上传到Paperclip

时间:2022-01-12 23:01:28

I'm trying to upload an image from an iOS device to a Rails API that uses paperclip to store images onto AWS. Everything works fine when I use postman to send the API call. This are the heroku logs when I call the request from postman...

我正在尝试从一个iOS设备上上传一个图片到一个使用paperclip将图片存储到AWS上的Rails API。当我使用postman发送API调用时,一切正常。这是我给邮递员打电话时的heroku日志……

Processing by Api::V1::FeedsController#create as JSON
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x007f1f7d30c5c0 @tempfile=#<Tempfile:/tmp/RackMultipart20160809-3-c6leue.jpg>, @original_filename="458989879.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"458989879.jpg\"\r\nContent-Type: image/jpeg\r\n">, "subdomain"=>"api", "uid"=>"6"}
Can't verify CSRF token authenticity
Geokit is using the domain: 
Command :: file -b --mime '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-ovx51i.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]' 2>/dev/null
Command :: identify -format %m '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]'
Command :: convert '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]' -auto-orient -resize "300x300>" '/tmp/b4a012d2ad09b09e0ccb3d15a438648a20160809-3-vvgjkn'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]' 2>/dev/null
Command :: identify -format %m '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]'
Command :: convert '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-1j7oszw.jpg[0]' -auto-orient -resize "100x100>" '/tmp/b4a012d2ad09b09e0ccb3d15a438648a20160809-3-19eu3y9'
(2.3ms)  BEGIN
Command :: file -b --mime '/tmp/5c6db5b2d12287ce12f1345f27b0ac2f20160809-3-k5lzw7.jpg'
SQL (4.5ms)  INSERT INTO "feeds" ("user_id", "message", "image_file_name", "image_content_type", "image_file_size", "image_updated_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["user_id", 6], ["message", "wow"], ["image_file_name", "458989879.jpg"], ["image_content_type", "image/jpeg"], ["image_file_size", 78487], ["image_updated_at", "2016-08-09 14:39:57.766228"], ["created_at", "2016-08-09 14:39:57.985558"], ["updated_at", "2016-08-09 14:39:57.985558"]]
[paperclip] saving /feeds/images/000/000/018/original/458989879.jpg
[AWS S3 200 0.110633 0 retries] put_object(:acl=>:public_read,:bucket_name=>"petoye",:content_length=>78487,:content_type=>"image/jpeg",:data=>Paperclip::UploadedFileAdapter: 458989879.jpg,:key=>"feeds/images/000/000/018/original/458989879.jpg")  
[paperclip] saving /feeds/images/000/000/018/thumb/458989879.jpg
[paperclip] saving /feeds/images/000/000/018/medium/458989879.jpg
(1.7ms)  COMMIT
[AWS S3 200 0.071674 0 retries] put_object(:acl=>:public_read,:bucket_name=>"petoye",:content_length=>8944,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: b4a012d2ad09b09e0ccb3d15a438648a20160809-3-19eu3y9,:key=>"feeds/images/000/000/018/thumb/458989879.jpg")  
[AWS S3 200 0.037868 0 retries] put_object(:acl=>:public_read,:bucket_name=>"petoye",:content_length=>41611,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: b4a012d2ad09b09e0ccb3d15a438648a20160809-3-vvgjkn,:key=>"feeds/images/000/000/018/medium/458989879.jpg")  
Completed 201 Created in 462ms (Views: 2.5ms | ActiveRecord: 8.6ms)

When I try doing this from an simulator ...

当我在模拟器上做这个的时候……

Processing by Api::V1::FeedsController#create as JSON
Parameters: {"test"=>"hi", "file"=>#<ActionDispatch::Http::UploadedFile:0x007f1f7ddb6390 @tempfile=#<Tempfile:/tmp/RackMultipart20160809-3-1rdm92u.png>, @original_filename="test.png", @content_type="image/png", @headers="Content-Disposition:form-data; name=\"file\"; filename=\"test.png\"\r\nContent-Type: image/png\r\n">, "subdomain"=>"api", "uid"=>"6"}
Geokit is using the domain: 
(0.7ms)  BEGIN
Started POST "/feeds/6/create" for  at 2016-08-09 14:43:45 +0000
(0.6ms)  ROLLBACK
Completed 422 Unprocessable Entity in 6ms (Views: 2.5ms | ActiveRecord: 1.3ms)
Can't verify CSRF token authenticity
at=info method=POST path="/feeds/6/create" host= request_id=b4d0506f-a1ce-477e-993b-fc39e338e0fc fwd="" dyno=web.1 connect=1ms service=37566ms status=422 bytes=435

This is my code when I upload the image using Swift

这是我使用Swift上传图片时的代码

func UploadRequest()
{
    let url = NSURL(string: "")

    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"

    let boundary = generateBoundaryString()

    //define the multipart request type

    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    if (postImage.image == nil)
    {
        return
    }

    let image_data = UIImagePNGRepresentation(postImage.image!)


    if(image_data == nil)
    {
        return
    }


    let body = NSMutableData()

    let fname = "test.png"
    let mimetype = "image/png"

    //define the data post parameter

    body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition:form-data; name=\"test\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("hi\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)



    body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(image_data!)
    body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)


    body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)



    request.HTTPBody = body



    let session = NSURLSession.sharedSession()


    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        guard error == nil && data != nil else {                                                          // check for fundamental networking error
            print(error!)
            return
        }

        if let httpStat = response as? NSHTTPURLResponse where httpStat.statusCode == 201
        {
            //pop up comment added


        }


        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 201 {           // check for http errors
            print("statusCode should be 201, but is \(httpStatus.statusCode)")
            print(response!)
        }

        var responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(responseString!)


    }
    task.resume()

}

I'm stuck on this for a while now and haven't really found a question that works for paperclip as well as swift, so please suggest me something that works for both.

我在这个问题上已经有一段时间了,还没有找到一个对paperclip和swift都有效的问题,所以请给我推荐一些对双方都有效的方法。

2 个解决方案

#1


1  

I did the following edits to the body block which I posted in my question above..

我对我在上面的问题中发布的body block做了如下的编辑。

body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"image\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(image_data!)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

And then I kind of figured out what was going wrong..

然后我发现了问题所在。

I was trying to upload a default image which was heavily sized from the iOS simulator. So I deployed the app to my iPhone device and tried uploading a relatively small sized image and the upload was successful.

我试着上传一个默认的图片,这个图片在iOS模拟器中很大。所以我把应用部署到我的iPhone设备上,并尝试上传一个相对较小的图片,上传成功。

So I went to my feed model on my rails API where I was taking the image. This was the code.

于是我在rails API上创建了feed模型,在那里我得到了图像。这是代码。

has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
validates_with AttachmentSizeValidator, attributes: :image, less_than: 1.megabytes
validates_with AttachmentPresenceValidator, attributes: :image

As you see in the third line I had restricted the image sizes to less than 1Mb, so I just removed that line and everything works fine now.

正如您在第三行中看到的,我将图像大小限制为小于1Mb,所以我删除了这一行,现在一切正常。

#2


0  

 let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)

mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
let boundaryConstant = "myRandomBoundary12345";
        let contentType = "multipart/form-data;boundary="+boundaryConstant
        mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
        let uploadData = NSMutableData()

        for (key, value) in files {
            if let data = value.data{
                uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
                uploadData.appendData(String("Content-Disposition: form-data; name=\"" + key + "\"; filename=\""+value.fileName+"\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
                if let contentType = value.contentType{
                    uploadData.appendData(("Content-Type: " + contentType + "\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
                }
                uploadData.appendData(data)
            }
        }
        if let params = parameters {
            for (key, value) in params {
                uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
                uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
            }
        }
        uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)

#1


1  

I did the following edits to the body block which I posted in my question above..

我对我在上面的问题中发布的body block做了如下的编辑。

body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"image\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(image_data!)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

And then I kind of figured out what was going wrong..

然后我发现了问题所在。

I was trying to upload a default image which was heavily sized from the iOS simulator. So I deployed the app to my iPhone device and tried uploading a relatively small sized image and the upload was successful.

我试着上传一个默认的图片,这个图片在iOS模拟器中很大。所以我把应用部署到我的iPhone设备上,并尝试上传一个相对较小的图片,上传成功。

So I went to my feed model on my rails API where I was taking the image. This was the code.

于是我在rails API上创建了feed模型,在那里我得到了图像。这是代码。

has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
validates_with AttachmentSizeValidator, attributes: :image, less_than: 1.megabytes
validates_with AttachmentPresenceValidator, attributes: :image

As you see in the third line I had restricted the image sizes to less than 1Mb, so I just removed that line and everything works fine now.

正如您在第三行中看到的,我将图像大小限制为小于1Mb,所以我删除了这一行,现在一切正常。

#2


0  

 let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)

mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
let boundaryConstant = "myRandomBoundary12345";
        let contentType = "multipart/form-data;boundary="+boundaryConstant
        mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
        let uploadData = NSMutableData()

        for (key, value) in files {
            if let data = value.data{
                uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
                uploadData.appendData(String("Content-Disposition: form-data; name=\"" + key + "\"; filename=\""+value.fileName+"\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
                if let contentType = value.contentType{
                    uploadData.appendData(("Content-Type: " + contentType + "\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
                }
                uploadData.appendData(data)
            }
        }
        if let params = parameters {
            for (key, value) in params {
                uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
                uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
            }
        }
        uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)