通过多部分表单数据在服务器上发送视频

时间:2016-07-22 12:01:42

标签: ios objective-c swift request nsmutableurlrequest

我在服务器上推送视频时遇到问题。我的API在正文中有两个参数:详细信息和文件,我必须通过标题中的标记对其进行授权。

在开始时,我的文件已准备好并导出到我推送到方法的URL。接下来是详细信息(字符串值)我正在尝试准备它在服务器上发送。

运行我的应用后,我遇到了这个错误:

  

错误域= NSCocoaErrorDomain代码= 3840"没有值。" UserInfo = {NSDebugDescription =无值。}

响应中的服务器返回有关JSON中推送视频的所有数据

我使用本教程尝试迈出第一步: https://mindfiremobile.wordpress.com/2014/01/27/upload-video-to-server-using-multiparts/

这是我在服务器上推送视频的第一步,我很乐意提供帮助。

func postDetailsWithVideo(details: String, file: NSURL) {
    let url = serverURL.URLByAppendingPathComponent("api/details")

    let videoData = NSData.init(contentsOfURL: file)

    let kBoundary = "---------------------------14737809831466499882746641449"
    let kStartTag = "--%@\r\n"
    let kEndTag = "\r\n"
    let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"

    let body = NSMutableData()

    //details data
    body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(String(format: kContent, "storyDetails").dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(details.dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)

    //Video data
    body.appendData(String(format: kStartTag, kBoundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition: form-data; file=\"flv\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Type: application/octet-stream\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(NSData(data: videoData!))
    body.appendData(String(format: kEndTag).dataUsingEncoding(NSUTF8StringEncoding)!)

    // close form
    body.appendData("--\(kBoundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

    let contentType = "multipart/form-data; boundary=\(kBoundary)"

    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.setValue("Bearer \(DataManager().getAccessToken())", forHTTPHeaderField: "Authorization")
    request.setValue(contentType, forHTTPHeaderField: "Content-Type")
    request.HTTPBody = body

    print(body)

    postDataOnTheServer(request, apiMethod: "api/details")

}

func postDataOnTheServer(request: NSMutableURLRequest, apiMethod: NSString) {
        let currentRequest = request

        let task = NSURLSession.sharedSession().dataTaskWithRequest(currentRequest){ data, response, error in
            if error != nil{
                self.delegate?.errorOccured(apiMethod, error: error!)
                return
            }

            do {
                let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

                self.delegate?.serverResponseFromAPIMethod(apiMethod, result: result!)

            } catch {
                print("response - some error")
                print(error) //do something with me
            }
        }

        task.resume()
    }

1 个答案:

答案 0 :(得分:1)

它是一个服务器问题。请你的后端不要回应任何东西,你会收到回应。