Swift3视频文件上传以及另外一个表单参数(可以从形式跳过并以URL路径发送)



我已经搜索了很长时间,但没有找到有关如何执行此操作的任何良好资源。API期望一个表单参数" user_id" (现在将其urlPath而不是形式参数发送)和其他"文件"和其他"文件"。请使用urlsession任务或iOS的任何库提供一些代码示例。

尝试Alamofire:

 Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(self.fileurl, withName: "file")
    }, to:"http://www.www.www/upload/8590",
       headers: ["Authorization": "Bearer (SharedPreferences.preferences.getKeyValue(key: Constants.AUTH_TOKEN_key))"] )
    { (result) in
        switch result {
        case .success(let upload, _ , _):
            upload.uploadProgress(closure: { (progress) in
                print("uploding>>>>>>")
            })
            upload.responseJSON { response in
                print(response)
                print("done")
            }
        case .failure(let encodingError):
            print("failed")
            print(encodingError)
        }

这将从服务器中抛出500个,说它无法使用UTF-8编码读取标题。

    message = "An Error OccuredInvalid header string: 'utf8' codec can't decode byte 0x9b in position 1: invalid start byte";
result = "Traceback (most recent call last):n  File "/var/www/wb_ios/wb_app/views.py", line 94, in postn    user_auth = jwt_decode_handler(auth).get('sub')n  File "/usr/local/lib/python2.7/dist-packages/rest_framework_jwt/utils.py", line 104, in jwt_decode_handlern    unverified_payload = jwt.decode(token, None, False)n  File "/usr/local/lib/python2.7/dist-packages/jwt/api_jwt.py", line 70, in decoden    payload, signing_input, header, signature = self._load(jwt)n  File "/usr/local/lib/python2.7/dist-packages/jwt/api_jws.py", line 177, in _loadn    raise DecodeError('Invalid header string: %s' % e)nDecodeError: Invalid header string: 'utf8' codec can't decode byte 0x9b in position 1: invalid start byten";
status = 500;

}

此外,我可以使用Postman成功发送视频。使用图像中所示的form-data字段。身体标题

最后我也尝试了:

let url = NSURL(string: "http://www.www.www/upload/8590")
    let request = NSMutableURLRequest(url: url! as URL)
    let boundary = "------------------------your_boundary"
    request.httpMethod = "POST"
    request.setValue("ios", forHTTPHeaderField: "client")
    request.setValue(Constants.AUTH_KEY, forHTTPHeaderField: Constants.AUTH_KEY_key)
    request.setValue("Bearer " + SharedPreferences.preferences.getKeyValue(key: Constants.AUTH_TOKEN_key)!, forHTTPHeaderField: Constants.AUTH_AUTHORIZATION_key)
    request.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content-Type")
    var movieData: NSData?
    do {
        movieData = try NSData(contentsOfFile: fileurl.path, options: NSData.ReadingOptions.alwaysMapped)
        print(movieData)
    } catch _ {
        movieData = nil
        return
    }
    let body = NSMutableData()
    // change file name whatever you want
    let filename = "upload.mov"
    let mimetype = "video/mov"
    body.append("--(boundary)rn".data(using: String.Encoding.utf8)!)
    body.append("Content-Disposition:form-data; name="file"; filename="(filename)"rn".data(using: String.Encoding.utf8)!)
    body.append("Content-Type: (mimetype)rnrn".data(using: String.Encoding.utf8)!)
    body.append(movieData! as Data)
    request.httpBody = body as Data
    let session = URLSession.shared
    let task = session.dataTask(with: request as URLRequest) {
        (data, response, error) in
        guard let _:NSData = data as! NSData, let _:URLResponse = response, error == nil else {
            print("error")
            return
        }
        let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print(dataString)
    }
    task.resume()

这也引发这样的错误:

{"status":500,"message":"An Error Occuredu'file'","result":"Traceback (most recent call last):n  File "/var/www/wb_ios/wb_app/views.py", line 104, in postn    file_obj = request.data['file']n  File "/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py", line 85, in __getitem__n    raise MultiValueDictKeyError(repr(key))nMultiValueDictKeyError: "u'file'"n"})

swift3.0

Alamofire.upload(multipartFormData: { MultipartFormData in
            for (key, value) in parameter {
                MultipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }
// here you can upload only mp4 video
                multipartFormData.append(self.fileurl!, withName: "file", fileName: "video.mp4", mimeType: "video/mp4")
// here you can upload any type of video            
                 multipartFormData.append((self.fileurl.data(using: String.Encoding.utf8, allowLossyConversion: false))!, withName: "File")
            print(MultipartFormData)
        },to:"http://www.www.www/upload/8590",headers: ["Authorization": "Bearer (SharedPreferences.preferences.getKeyValue(key: Constants.AUTH_TOKEN_key))"]
            )
        { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (progress) in
                    print("Upload Progress: (progress.fractionCompleted)")
                })
                upload.responseJSON { response in
                    print(response.result.value ?? String())
                    print(response.data ?? NSData())
                    // send to completion block
                    completion(response.data as AnyObject? ?? NSData())
                }
            case .failure(let encodingError):
                print(encodingError)
                errorOccured(encodingError as NSError?)
            }
        }

相关内容

  • 没有找到相关文章

最新更新