web-dev-qa-db-fra.com

Importer une image avec iOS multipartite dans Swift

je rencontre un problème de téléchargement d'image avec multipart-form

voici mon code que j’ai utilisé à partir de ceci réponse

    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

alors j'utilise NSURLSession pour appliquer la demande

le serveur dit que je n'ai pas choisi l'image à télécharger, je veux seulement télécharger l'image pour l'instant 

dois-je utiliser des chemins d'images pour télécharger une image ou ses données suffisent?

est-ce que je manque quelque chose, n'importe quelle aide pour comprendre ceci?

16
Amr Mohamed

Ma version qui fonctionne à 100%. Cela vous aidera peut-être.

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
}
11
Anthony Akentiev
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()

}
4
jansma

J'ai implémenté Charger une image en utilisant plusieurs parties dans Swift 4:

Voici le code. Jetez un coup d'oeil s'il vous plait

//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)"
}
1
Mannam Brahmam
class func postMultiPartdata( postdatadictionary: [AnyHashable: Any], apikey: String, completion: @escaping (Any) -> () ) {

        if Utils().isConnectedToNetwork() == false
        {
            Utils().showMessage("Check internet")
            return
        }

        let strURL = "http://redspark.biz/dropp/api/\(apikey)"

        let url = URL(string: strURL)
        var urlRequest = URLRequest(url: url!)

        urlRequest.httpMethod = "POST"

        let body = NSMutableData();
        let boundary = "---------------------------14737809831466499882746641449"
        let contentType = "multipart/form-data; boundary=\(boundary)"
        urlRequest.addValue(contentType, forHTTPHeaderField: "Content-Type")

        for (key, value) in postdatadictionary {

            if(value is Data)
            {
                let  TimeStamp = "\(Date().timeIntervalSince1970 * 1000)"

                body.append("--\(boundary)\r\n".data(using: .utf8)!)
                body.append("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(TimeStamp)\"\r\n".data(using:.utf8)!)
                body.append("field_mobileinfo_image\r\n".data(using: .utf8)!)

                body.append("--\(boundary)\r\n".data(using: .utf8)!)
                body.append("Content-Disposition: form-data; name=\"files[field_mobileinfo_image]\"; filename=\"img.jpg\"\r\n".data(using: .utf8)!)
                body.append("Content-Type: image/jpeg\r\n\r\n".data(using: .utf8)!)

//                var imgData: Data? = nil
//                if let aKey = value as? Data {
//                    imgData = NSData(data: aKey) as Data
//                }

                body.append(value as! Data)

            }
            else
            {
                if let anEncoding = "--\(boundary)\r\n".data(using: .utf8) {
                    body.append(anEncoding)
                }
                if let anEncoding = "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8) {
                    body.append(anEncoding)
                }
                if let aKey = postdatadictionary[key], let anEncoding = "\(aKey)".data(using: .utf8) {
                    body.append(anEncoding)
                }
                if let anEncoding = "\r\n".data(using: .utf8) {
                    body.append(anEncoding)
                }
              }
            }

        if let anEncoding = "--\(boundary)--\r\n".data(using: .utf8) {
            body.append(anEncoding)
        }
        // setting the body of the post to the reqeust
        urlRequest.httpBody = body as Data

        URLSession.shared.dataTask(with:urlRequest) { (data, response, error) in
            if error != nil {

                print(error!)
                completion("")
            } else {
                var dictonary:NSDictionary?
                do {
                    dictonary = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
                    if let myDictionary = dictonary
                    {
                        completion(myDictionary)
                    }
                } catch let error as NSError {
                    completion(error)
                }
            }

            Utils().HideLoader()

            }.resume()

        }
0
Ishwar Hingu

La première chose que j'ai remarquée est le application/octet-stream en tant que Conten-Type, il est généralement utilisé lorsque le type de fichier est inconnu. Certains frameworks/bibliothèques Web rejetteront ce type de contenu si une image est requise.

Deuxièmement, je ne vois nulle part la longueur du message, essayez de l'ajouter:

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

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