【问题标题】:Alamofire upload "name" parameterAlamofire 上传“名称”参数
【发布时间】:2016-06-17 01:29:26
【问题描述】:

在 Alamofire upload 代码中有一个 namefileName 字段。

Alamofire.upload(
                    .POST,
                    "http://192.168.1.241:8080/file/user",
                    multipartFormData: { multipartFormData in
                        multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file", mimeType: "image/jpeg")
                    },
                    encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .Success(let upload, _, _):
                            upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                                dispatch_async(dispatch_get_main_queue()) {
                                    let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
                                    //progress(percent: percent)
                                    print(percent)
                                }
                            }
                            upload.responseJSON { response in
                                debugPrint(response)
                            }
                        case .Failure(let encodingError):
                            debugPrint(encodingError)
                        }
                    }
                    )

在我的 Spring 服务器中,我正在寻找 @RequestParam("file") MultipartFile file,但我无法让它工作,因为我将 name 设置为 file。但显然我需要将fileName 设置为"file"

如果name 似乎没有用于请求参数映射,那么它的用途究竟是什么?

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    对于“file”,它表示您文件的完整 NSURL 属性(例如:www.mysite.com/myvideo.mp4),对于“filename”只有String 格式的lastPathComponent(例如:video.mp4)

    【讨论】:

    • 但是当你已经传递了文件的NSData 时,为什么还需要文件的NSURL 属性呢?
    • 因为当NSData只包含文件数据时,NSURL属性可以由一些参数组成,所以来源通过MultipartFormData的econding很好地区分请求。 Alamofire 是一个基于 NSMutableURLRequest 的复杂框架。
    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 2016-10-06
    • 2023-03-22
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2014-11-25
    相关资源
    最近更新 更多