[英]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: 这是工作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.