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

标签 ios iphone swift file-upload alamofire

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

What i have done so far is below

请求服务:
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";
}

Can anyone please tell me what i am doing wrong with request ?

最佳答案

尝试:

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

关于ios - 使用多部分请求上传文件 - Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50348265/

相关文章:

iphone - 如何在 iOS 中重用代码(IBAction)?

ios - Swift 3 - 排序数组/字典 : '(AnyObject)' is not a subtype of 'NSString'

ios - 为 UIGestureRecognizer 选择器操作传递参数

ios - Imagemagick降低PNG深度

ios - 即使提供布局后,UICollectionViewDelegateFlowLayout 也会出错

iphone - 动态获取键盘的框架

iphone - 支持不同大小单元格的 iOS GridView

ios - 使用 UIViewController 子类作为参数设置按钮目标

ios - 两个不同的tableview xCode之间的冲突

ios - 如何为每条线段添加颜色?