1

I am writing the following method to upload image you server.

func uploadImageOne(image: UIImageView){
        var imageData = UIImagePNGRepresentation(image.image)

        if imageData != nil{
            var request = NSMutableURLRequest(URL: NSURL(string:"http://abcd-site.com/sat/upload.aspx")!)
            var session = NSURLSession.sharedSession()

            request.HTTPMethod = "POST"

            var boundary = NSString(format: "---------------------------14737809831466499882746641449")
            var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
            //  println("Content Type \(contentType)")
            request.addValue(contentType, forHTTPHeaderField: "Content-Type")

            var body = NSMutableData.alloc()

            // Title
            body.appendData(NSString(format: "\r\n--%@\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData(NSString(format:"Content-Disposition: form-data; name=\"title\"\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData("Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)

            // Image
            body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData(NSString(format:"Content-Disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData(imageData)
            body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)

            request.HTTPBody = body

            var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)

            var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)

            println("returnString \(returnString)")

        }

I am getting following error.

Optional(Error:The SaveAs method is configured to require a rooted path, and the path '~/ContentImages/ImagesDir/7ece04d7-ff77-4c2c-9ce2-df7e52098b7b.jpg' is not rooted.

What is expected cause of ERROR.

and it also causes stuck in SCREEN, what is proper way to upload UIImage to server.

Thanks

Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
  • This is an error coming back from your server, best to ask them what the issue is. – David Berry Mar 07 '15 at 06:19
  • I asked them, soon they will reply, but what about stuck issue, it is causing stuck to main screen @DavidBerry – Sam Shaikh Mar 07 '15 at 06:20
  • You send a synchronous request. This means it blocks the current thread until the operation has finished. You likely call the function from the main thread causing the main thread to be blocked. Typically, you would either send a asynchronous request (using NSURLConnection's delegate to get informed about the response) or put the call into a NSOperationQueue. – Florian Friedrich Mar 07 '15 at 06:31
  • @ffried All of that is true, but doesn't explain the error he's seeing. But yes, the OP should re-think the strategy used to use an asynchronous connection mechanism, assuming they're not executing this function on a background thread in the first place. – David Berry Mar 07 '15 at 06:44
  • 1
    @DavidBerry The error is definitively coming from the server. It's a C# error. See http://stackoverflow.com/questions/1206662/the-saveas-method-is-configured-to-require-a-rooted-path-and-the-path-fp-is-n – Florian Friedrich Mar 07 '15 at 07:57

0 Answers0