Ios 在Swift中通过HTTP Post请求发送文件
我对Swift还很陌生,我正试图从iOS上传一些文件到raspberry pi。我找到了HTTP post请求,但我对它也不太了解。我实际上是一个嵌入式linux程序员。所以,我是这样发布我的请求的Ios 在Swift中通过HTTP Post请求发送文件,ios,swift,file,http,post,Ios,Swift,File,Http,Post,我对Swift还很陌生,我正试图从iOS上传一些文件到raspberry pi。我找到了HTTP post请求,但我对它也不太了解。我实际上是一个嵌入式linux程序员。所以,我是这样发布我的请求的 var data = Data() data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!) data.append("Content-Disposition: form-data;
var data = Data()
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(paramName)\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
data.append("Content-Type: \"content-type header\"\r\n\r\n".data(using: .utf8)!)
data.append(read().data(using: .utf8)!)
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
很明显,若文件是txt文件,我就可以很好地工作,而且我无论如何也找不到以这种数据格式读取和放置原始数据的文件。有什么帮助吗
read()->string我无法解释我做了什么,但我试着写清楚
func getFile(fileName: String) -> [UInt8]? {
// See if the file exists.
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(fileName)
do {
// Get the raw data from the file.
let rawData: Data = try Data(contentsOf: fileURL)
// Return the raw data as an array of bytes.
return [UInt8](rawData)
} catch {
// Couldn't read the file.
return nil
}
}
print("\(fileName) not exist")
return nil
}
func uploadFile(fileName: String, fileExtension: String, endpoint: String) {
let fileNameWithExtension = fileName + "." + fileExtension
let url = URL(string: endpoint)
var data = Data()
// generate boundary string using a unique per-app string
let boundary = UUID().uuidString
let session = URLSession.shared
// Set the URLRequest to POST and to the specified URL
var urlRequest = URLRequest(url: url!)
urlRequest.httpMethod = "POST"
// Set Content-Type Header to multipart/form-data, this is equivalent to submitting form data with file upload in a web browser
// And the boundary is also set here
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
// Add the file data to the raw http request data
// data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
// data.append("Content-Disposition: form-data; name=\"name\"; username=\"kemal\"\r\n".data(using: .utf8)!)
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"file\"; filename=\"recieved\(fileExtension)\"\r\n".data(using: .utf8)!)
data.append("Content-Type: \"content-type header\"\r\n\r\n".data(using: .utf8)!)
print("opening file...")
if let bytes: [UInt8] = getFile(fileName: fileNameWithExtension) {
for byte in bytes {
data.append(byte)
}
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
// Send a POST request to the URL, with the data we created earlier
session.uploadTask(with: urlRequest, from: data, completionHandler: { responseData, response, error in
if error == nil {
let jsonData = try? JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let json = jsonData as? [String: Any] {
print(json)
}
}
}).resume()
}
}
要发送二进制文件,您通常会将其编码为base64上载文件。。。图像?文本文件?XML文件?这有关系吗?