我尝试使用Swift 3将图像上传到iOS服务器,我已尝试使用Alamofire,但它无法正常工作,所以我只是在这个论坛中搜索了另一个解决方案但没有运气。
我发现一些答案说该问题可能是服务器端,但是,在Android上图像正在正确上传。
这是我在swift 3中的上传功能:
func uploadImage(image: UIImage){
let imageData = UIImageJPEGRepresentation(image, 0.1)!
let session = URLSession(configuration: URLSessionConfiguration.default)
guard let url = URL(string: uploadPicUrl) /* your API url */) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "---------------------------14737809831466499882746641449"
let contentType = "multipart/form-data; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var body = Data()
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"userfile\"; filename=\"img.jpg\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Transfer-Encoding: binary\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(imageData)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body
print("request", request.debugDescription)
print("body", body.debugDescription)
let dataTask = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Something went wrong: \(error)")
}
if let response = response {
print("Response: \n \(response)")
}
}
dataTask.resume()
}
答案 0 :(得分:0)
不使用Alamofire,您可以执行以下操作:
func uploadImage(chosenimage: UIImage) {
let url = ApiList.base_url + ApiList.uploadFile_Url
let myUrl = NSURL(string: url)
let image_data = UIImagePNGRepresentation(chosenimage)
let tempData = NSMutableData()
let request = NSMutableURLRequest(url:myUrl! as URL)
request.httpMethod = "POST"
let boundary = NSUUID().uuidString
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField:"Content-Type")
let mimetype = "image/png"
let fname = "test.png"
self.body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
self.body.append("Content-Disposition:form-data; name=\"profileUrl\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
self.body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
self.body.append(image_data!)
self.body.append("\r\n".data(using: String.Encoding.utf8)!)
let accessToken = UserDefaults.standard.value(forKey: "accessToken") as? String ?? ""
let deviceToken = UserDefaults.standard.value(forKey: "deviceToken") as? String ?? singletonclass.instance.getDeviceToken
let param = [
"accessToken":accessToken,
"deviceId":deviceToken,
"deviceType":"2"
]
for (key,value) in param {
tempData.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
tempData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
tempData.append("\(value)\r\n".data(using: String.Encoding.utf8)!)
}
self.body.append(tempData as Data)
self.body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = self.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 { return }
do
{
let responseDict = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
//print("\n tyiukmqw",responseDict)
let code = responseDict.value(forKey: "code") as? String
let message = responseDict.value(forKey: "Message") as? String
singletonclass.instance.isrofilePicChagedOrNot = false
if code == "200"
{
print("success code")
DispatchQueue.main.async(execute: {
self.userProfile.image = chosenimage
UserDefaults.standard.setValue(UIImagePNGRepresentation(chosenimage), forKey: "UserProfilePicture")
singletonclass.instance.userProPic = chosenimage
})
}
else
{
DispatchQueue.main.async(execute: {
singletonclass.instance.showAlert(message!)
self.isActivityIndicatorNeed(false)
})
}
}
catch
{
}
}
task.resume()
}
答案 1 :(得分:0)
注意:此功能是使用Alamofire上传多个不同类型的文件
// upload file to server
func uploadFiles(files: [Data],completion : @escaping uploadHandler) {
let header : HTTPHeaders = ["Content-Type" : "application/x-www-form-urlencoded"] // if there's Authorization, you may add it in header
let url = URL(string: "Enter the url here")
Alamofire.upload(multipartFormData: { (multipartFormData) in
for document in files {
let fileName = NSUUID().uuidString
multipartFormData.append(document, withName: "documents", fileName:fileName ,mimeType: "image/jpeg") // You'll have to define the proper mime time for uploading other type of files. You may achieve it by creating a struct and storing the type of each file.
}
}, usingThreshold: UInt64.init(), to:url, method: .post, headers: header) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .success(_) : completion(true, nil)
case .failure(let error) :
print("Error in upload: \(error.localizedDescription)")
completion(false, nil)
}
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
completion(false,error)
}
}
}
typealias uploadHandler = (_ status :Bool,_ error :Error?)->()// Define this anywhere globally
在调用我传递文件数组的函数时,我将不同类型的文件转换为数据并将其上传到服务器。
如果你想要上传图像数组而不是调用函数之前。
var documents = [Data]()
for image in imageArray {
if let imgData = UIImageJPEGRepresentation(image, 1.0) {
documents.append(document)
}
}
现在您可以调用上传功能并传递文档并收听完成处理程序。
祝你好运。