简体   繁体   English

如何通过Swift使用multipart / form-date将文件发送到服务器

[英]How to send a file to the server using multipart/form-date via Swift

How do I send a file to the server using the Swift language? 如何使用Swift语言将文件发送到服务器? I send the file to the server and the global variable $_FILES (PHP) does not see the downloaded file. 我将文件发送到服务器,并且全局变量$ _FILES(PHP)看不到下载的文件。 In this case, if I send a request from any other Sender Package, (for example ARC). 在这种情况下,如果我从其他任何发件人程序包(例如ARC)发送请求。 Then the global array successfully displays the transferred POST by the file method. 然后,全局阵列通过文件方法成功显示了传输的POST。 What's wrong with that ?? 那是怎么了?

            // SEND DATA
            let boundary = "--------------------------684819564013531921146535"

            data = AppDelegate.createBody(boundary: boundary,
                                          data: data,
                                          mimeType: "image/png",
                                          filename: "FF4D00-0.8.png")

            let headers = [
                "Content-Type":"multipart/form-data; boundary=\(boundary)",
                "Content-Length":"\(data.count)"
            ]

            HttpURLConnection.request(type: 1, command: "upload/img", headers: headers, body: data, complition: { (error, data) in
                if error == nil && data != nil {                   
                      print(data)                                   
                }                                                    
             })

An here is the function that creates the body of the request 这里是创建请求主体的函数

// create body for http request
static func createBody(boundary: String,
                data: Data,
                mimeType: String,
                filename: String) -> Data {

    var body = Data()
    body.append("\(boundary)\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
    body.append("Content-Disposition: form-data; name=\"file\"; filename=\"FF4D00-0.8.png\"\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
    body.append("Content-Type: \(mimeType)\r\n\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
    body.append("P".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
    body.append("\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
    body.append(boundary.appending("--").data(using: String.Encoding.utf8, allowLossyConversion: true)!)

    print("size is \(body.count)")

    return body as Data
}

Here is work: 这是工作:

example result sent file via ARC 通过ARC发送的示例结果文件

I'm using Alamofire library. 我正在使用Alamofire库。 Well documentated, see for example: 有据可查,例如:

 let imageData = UIImagePNGRepresentation(image)! Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in debugPrint(response) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Ajax、PHP 和 jQuery 将多部分/表单数据发送到服务器? - How to send multipart/form-data to server using Ajax, PHP and jQuery? 如何在Android中使用多实体将JSONArray字符串发送到PHP服务器 - How to send JSONArray string to php server using multipart entity in android 如何使用 HTTP POST multipart/form-data 将文件上传到服务器? - How to upload file to server with HTTP POST multipart/form-data? 如何通过form_open_multipart将id从视图发送到Controller CodeIgniter - How to send id from view to Controller CodeIgniter via form_open_multipart 使用cURL执行流(逐行)通过POST使用MULTIPART / FORM-DATA进行文件上传 - Using cURL to do a stream (line by line) FILE UPLOAD via POST with MULTIPART/FORM-DATA 如何使用Swift将图像发送到php文件 - How to send a image to a php file using Swift 使用改造发送带有某些参数的多部分(文件) - Send multipart(File) with some parameters using retrofit 上传通过 Wordpress 中的多部分表单数据发送的文件 - Uploading a file sent via multipart form data in Wordpress 如何将文件从表单输入发送到服务器 - How to send file from form input to server 通过使用jQuery提交多部分/表单数据的问题 - Problem via submit multipart/form-data using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM