0

I have uploaded video file to server but the file size is zero.

let url:NSURL? = NSURL(string: (Config.sampleuploadurl as String)+"/uploadServlet?chehara_email=\(candit_email)")
// let url:NSURL? = NSURL(string: (Config.apiUrl as String)+"/UploadServlet?chehara_email=\(candit_email)")
print(url)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"
let boundaryConstant = "Boundary-7MA4YWxkTLLu0UIW"; // This should be auto-generated.
let contentType = "multipart/form-data; boundary=" + boundaryConstant

let fileName:String = videoName

let mimeType = "text/MOV"
let fieldName = "uploadFile"

request.setValue(contentType, forHTTPHeaderField: "Content-Type")

var error: NSError?
var dataString = "--\(boundaryConstant)\r\n"

dataString += "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(fileName)\"\r\n"
print("dataString")

dataString += "Content-Type: \(mimeType)\r\n\r\n"
print(dataString)

dataString += String(contentsOfFile: videoName, encoding: NSUTF8StringEncoding, error: &error)!   // This is Error Line

dataString += "\r\n"
dataString += "--\(boundaryConstant)--\r\n"

let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
print("Request")
print(requestBodyData)

request.HTTPBody = requestBodyData
print(request.HTTPBody)

})

I refer this tutorial.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Manikandaprabu
  • 107
  • 1
  • 1
  • 5

1 Answers1

0

Eric, You should not use NSMutableURLRequest, You should use NSURLSession. Here is some I code I posted else

http://stackoverflow.com/questions/35604866/swift-send-file-to-server/35609325#35609325

This post I replied too shows its implementation. NSMutableURLRequest is being depreciated.

And here is a 1 of several excellent tutorials on the subject; and no I am not Bart Jakobs in disguise :)

http://code.tutsplus.com/tutorials/networking-with-nsurlsession-part-1--mobile-21394
user3069232
  • 8,587
  • 7
  • 46
  • 87