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

时间:2016-08-09 15:16:56

标签: ios swift paperclip multipartform-data rails-api

我尝试将图像从iOS设备上传到使用回形针将图像存储到AWS的Rails API。当我使用邮递员发送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)

当我尝试从模拟器中执行此操作时...

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

这是我使用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()

}

我现在已经坚持了一段时间,并且没有真正找到一个适用于回形针和快速的问题,所以请建议一些适合两者的东西。

2 个答案:

答案 0 :(得分:1)

我对我在上面的问题中发布的正文块进行了以下编辑。

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)!)

然后我发现出了什么问题......

我试图从iOS模拟器上传一个尺寸很大的默认图像。所以我将应用程序部署到我的iPhone设备上,尝试上传相对较小的图像,上传成功。

所以我在我的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

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

答案 1 :(得分: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)