Загрузка изображения с помощью iOS с несколькими формами данных в Swift

У меня проблема с загрузкой изображения с многочастной формой

вот мой код, который я использовал из этого answer

    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"

    var boundary = generateBoundaryString()
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    var body = NSMutableData()

    if self.img.image != nil {
        var imageData = UIImagePNGRepresentation(self.img.image)

        if imageData != nil {
            body.appendString("--\(boundary)\r\n")
            body.appendString("Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n")
            body.appendString("Content-Type: image/png\r\n\r\n")
            body.appendData(imageData!)
            body.appendString("\r\n")
        }

    }

    body.appendString("--\(boundary)--\r\n")
    request.setValue("\(body.length)", forHTTPHeaderField:"Content-Length")
    request.HTTPBody = body

то я использую NSURLSession для применения запроса

сервер говорит, что я не выбрал изображение для загрузки, я хочу только загрузить изображение

Мне нужно использовать пути изображений для загрузки любого изображения или его данных достаточно?

Пропустить любую вещь, любую помощь, чтобы понять это?

Ответ 1

Моя версия, на которой работает 100%. Возможно, это поможет вам.

let url = "http://server/upload"
let img = UIImage(contentsOfFile: fullPath)
let data: NSData = UIImageJPEGRepresentation(img, 1)

sendFile(url, 
    fileName:"one.jpg", 
    data:data, 
    completionHandler: completionHandler:{
        (result:Bool, isNoInternetConnection:Bool) -> Void in

            // ...     
            NSLog("Complete: \(result)")
    }
)


func sendFile(
    urlPath:String,
    fileName:String,
    data:NSData,
    completionHandler: (NSURLResponse!, NSData!, NSError!) -> Void){

        var url: NSURL = NSURL(string: urlPath)!
        var request1: NSMutableURLRequest = NSMutableURLRequest(URL: url)

        request1.HTTPMethod = "POST"

        let boundary = generateBoundary()
        let fullData = photoDataToFormData(data,boundary:boundary,fileName:fileName)

        request1.setValue("multipart/form-data; boundary=" + boundary,
            forHTTPHeaderField: "Content-Type")

        // REQUIRED!
        request1.setValue(String(fullData.length), forHTTPHeaderField: "Content-Length")

        request1.HTTPBody = fullData
        request1.HTTPShouldHandleCookies = false

        let queue:NSOperationQueue = NSOperationQueue()

        NSURLConnection.sendAsynchronousRequest(
            request1,
            queue: queue,
            completionHandler:completionHandler)
}

// this is a very verbose version of that function
// you can shorten it, but i left it as-is for clarity
// and as an example
func photoDataToFormData(data:NSData,boundary:String,fileName:String) -> NSData {
    var fullData = NSMutableData()

    // 1 - Boundary should start with --
    let lineOne = "--" + boundary + "\r\n"
    fullData.appendData(lineOne.dataUsingEncoding(
        NSUTF8StringEncoding,
        allowLossyConversion: false)!)

    // 2
    let lineTwo = "Content-Disposition: form-data; name=\"image\"; filename=\"" + fileName + "\"\r\n"
    NSLog(lineTwo)
    fullData.appendData(lineTwo.dataUsingEncoding(
        NSUTF8StringEncoding,
        allowLossyConversion: false)!)

    // 3
    let lineThree = "Content-Type: image/jpg\r\n\r\n"
    fullData.appendData(lineThree.dataUsingEncoding(
        NSUTF8StringEncoding,
        allowLossyConversion: false)!)

    // 4
    fullData.appendData(data)

    // 5
    let lineFive = "\r\n"
    fullData.appendData(lineFive.dataUsingEncoding(
        NSUTF8StringEncoding,
        allowLossyConversion: false)!)

    // 6 - The end. Notice -- at the start and at the end
    let lineSix = "--" + boundary + "--\r\n"
    fullData.appendData(lineSix.dataUsingEncoding(
        NSUTF8StringEncoding,
        allowLossyConversion: false)!)

    return fullData
}

Ответ 2

public func UPLOADIMG(url: String,parameters: Dictionary<String,AnyObject>?,filename:String,image:UIImage, success:((NSDictionary) -> Void)!, failed:((NSDictionary) -> Void)!, errord:((NSError) -> Void)!) {
        var TWITTERFON_FORM_BOUNDARY:String = "AaB03x"
        let url = NSURL(string: url)!
        var request:NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 10)
        var MPboundary:String = "--\(TWITTERFON_FORM_BOUNDARY)"
        var endMPboundary:String = "\(MPboundary)--"
        //convert UIImage to NSData            
        var data:NSData = UIImagePNGRepresentation(image)
        var body:NSMutableString = NSMutableString();
        // with other params
        if parameters != nil {
            for (key, value) in parameters! {
                body.appendFormat("\(MPboundary)\r\n")
                body.appendFormat("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
                body.appendFormat("\(value)\r\n")
            }
        }
        // set upload image, name is the key of image 
        body.appendFormat("%@\r\n",MPboundary)
        body.appendFormat("Content-Disposition: form-data; name=\"\(filename)\"; filename=\"pen111.png\"\r\n")
        body.appendFormat("Content-Type: image/png\r\n\r\n")
        var end:String = "\r\n\(endMPboundary)"
        var myRequestData:NSMutableData = NSMutableData();
        myRequestData.appendData(body.dataUsingEncoding(NSUTF8StringEncoding)!)
        myRequestData.appendData(data)
        myRequestData.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!)
        var content:String = "multipart/form-data; boundary=\(TWITTERFON_FORM_BOUNDARY)"
        request.setValue(content, forHTTPHeaderField: "Content-Type")
        request.setValue("\(myRequestData.length)", forHTTPHeaderField: "Content-Length")
        request.HTTPBody = myRequestData
        request.HTTPMethod = "POST"
        //        var conn:NSURLConnection = NSURLConnection(request: request, delegate: self)!
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {
            data, response, error in
            if error != nil {
                println(error)
                errord(error)
                return
            }
            var parseError: NSError?
            let responseObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError)
            if let responseDictionary = responseObject as? NSDictionary {
                success(responseDictionary)
            } else {
            }

        })
        task.resume()

}

Ответ 3

Первое, что я заметил, это application/octet-stream как Conten-Type, это обычно используется, когда тип файла неизвестен. Некоторые веб-фреймворки/библиотеки отклонят этот тип содержимого, если требуется изображение.

Во-вторых, я не могу видеть длину сообщения в любом месте, попробуйте добавить его:

body.appendString("--\(boundary)--\r\n")

// set the content-length
request.setValue("\(body.length)", forHTTPHeaderField:"Content-Length")

Ответ 4

Я реализовал загрузку изображения с помощью многочастного в Swift 4:

Вот код. Пожалуйста, посмотрите

//MARK: Uplaod User Profile Pic
func uploadImageToServerFromApp(nameOfApi : NSString, parameters : NSString, uploadedImage : UIImage, withCurrentTask :RequestType, andDelegate :AnyObject)->Void {
    if self.isConnectedToNetwork(){
        currentTask = withCurrentTask
        let myRequestUrl = NSString(format: "%@%@%@",GlobalConstants.KBaseURL,nameOfApi,parameters)
        let url = (myRequestUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))!
        var request : NSMutableURLRequest = NSMutableURLRequest()
        request = URLRequest(url: URL(string:url as String)!) as! NSMutableURLRequest
        request.httpMethod = "POST"
        let boundary = generateBoundaryString()
        //define the multipart request type
        request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
        let image_data = UIImagePNGRepresentation(uploadedImage)
        if(image_data == nil){
            return
        }
        let body = NSMutableData()
        let fname = "image.png"
        let mimetype = "image/png"
        //define the data post parameter
        body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
        body.append("Content-Disposition:form-data; name=\"image\"\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append("hi\r\n".data(using: String.Encoding.utf8)!)
        body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
        body.append("Content-Disposition:form-data; name=\"image\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
        body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append(image_data!)
        body.append("\r\n".data(using: String.Encoding.utf8)!)
        body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
        request.httpBody = body as Data
        let session = URLSession.shared
        let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
            guard let data = data, error == nil else {                                                 // check for fundamental networking error
                // print("error=\(String(describing: error))")
                self.showAlertMessage(title: "App name", message: "Server not responding, please try later")
                return
            }
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                // print("statusCode should be 200, but is \(httpStatus.statusCode)")
                // print("response = \(String(describing: response))")
                self.delegate?.internetConnectionFailedIssue()
            }else{
                do {
                    self.responseDictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary
                    // self.Responsedata = data as NSData
                    //self.responseDictionary = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: AnyObject] as NSDictionary;

                    self.delegate?.responseReceived()
                } catch {
                    //print("error serializing JSON: \(error)")
                }
            }
        }
        task.resume()
    }
    else{
        // print("Internet Connection not Available!")
        self.showAlertMessage(title: "App Name", message: "No Internet Connection..")
    }
}

func generateBoundaryString() -> String
{
    return "Boundary-\(NSUUID().uuidString)"
}