使用多部分请求上传文件 - Swift 4

时间:2018-05-15 10:45:57

标签: ios iphone swift file-upload alamofire

我必须使用多部分请求在服务器上上传文件。对于网络电话,我正在使用Alamofire。

  

到目前为止,我所做的是

请求服务
enter image description here

多部分请求: -

let headers: HTTPHeaders = [
            "Content-type": "multipart/form-data"
        ]
        let  fileData = Filedata() // getting data from local path

        let URL = try! URLRequest(url: "https://SomeUrl/upload", method: .post, headers: headers)
        Alamofire.upload(multipartFormData: { (multipartFormData) in

             //multipartFormData.append(fileData, withName: "image", fileName: "image", mimeType: "image/png")
               multipartFormData.append(fileData, withName: "file")

        }, with: URL, encodingCompletion: { (result) in

            switch result {
            case .success(let upload, _, _):

                upload.responseJSON { response in
                    print(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }

        })

响应: -

{ Status Code: 400, Headers {
    Connection =     (
        close
    );
    "Content-Type" =     (
        "application/json;charset=UTF-8"
    );
    Date =     (
        "Tue, 15 May 2018 10:34:15 GMT"
    );
    "Transfer-Encoding" =     (
        Identity
    );
} }
[Data]: 171 bytes
[Result]: SUCCESS: {
    error = "Bad Request";
    message = "Required request part 'file' is not present";
    path = "/files/safebolt.org/upload";
    status = 400;
    timestamp = "2018-05-15T10:34:15.715+0000";
}
  

任何人都可以告诉我我的请求有什么问题吗?

3 个答案:

答案 0 :(得分:2)

我创建了一个功能。希望它适合你。

[error] C:\my_path\app\services\BuildService.scala:72: type mismatch;
[error]  found   : Unit
[error]  required: scala.concurrent.Future[String]
[error]         }
[error]         ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[info] Compiling 1 Scala source to C:\my_path\restapi\target\scala-
       2.12\classes...
[error] C:\my_path\restapi\app\services\BuildService.scala:72: type 
        mismatch;
[error]  found   : Unit
[error]  required: scala.concurrent.Future[String]
[error]         }
[error]         ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

答案 1 :(得分:1)

尝试:

multipartFormData.append(fileData, withName: "file", fileName: "file", mimeType: "image/png")

答案 2 :(得分:0)

试试这个,这是一个适合我的例子。如果要转换编码64用于图像添加扩展名。如果只是发布数据和图像,这段代码可能有所帮助。

//为帖子请求创建参数

    let parameters: Parameters=[

        "ad_title":classText1.text!,
        "ad_description":classText2.text!,
        "ad_category":CategoryClass.text!,
        "ad_condition":classText3.text!,
        "ad_username":classText6.text!,
        "ad_usermobile":classText7.text!,
        "ad_city":classText8.text!,
        "ad_price":classText4.text!,
        "negotiable":classText5.text!,
        "image1":adImage1.image!,
        "image2":adImage2.image!,
        "image3":adImage3.image!,
        "image4":adImage4.image!


    ]
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in parameters {
            multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
        }


    }, usingThreshold: UInt64.init(), to: URL, method: .post) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print("Succesfully uploaded  = \(response)")
                if let err = response.error{

                    print(err)
                    return
                }

            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")

        }
    }