Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Can't get Uploads to work with Background Transfer Services

$
0
0

Hey everybody. I'm trying to wrap up an application that has a photo upload capabilities. I have to handle the situation where the file is still uploading while the application moves to the background/suspended/terminated states. iOS 7 introduces the "Background Transfer Service", which sounds awesome, as it will allow my uploads to continue through all these states.

I was able to get downloads to work just fine, but I'm not having much luck with uploads. All examples I've found online regarding the background transfer service deal with downloads, not uploads.

I'm getting an error in the DidCompleteWithError method of the delegate class: Error Domain=kCFErrorDomainCFNetwork Code=303

Digging around in apple docs shows that this is a kCFErrorHTTPParseFailure. From what i've read on the net, this is usually due to the request not being formatted correctly. I tried to make sure headers are set correctly, and that the NSUrlRequest is using POST, but looking at the monotouch class for NSUrlRequest, everything is read-only outside of the constructors, which do not allow me to specify headers or HTTP method.

Can anybody shed some light on this? I've spent way more time than I can afford on this so far.

string uploadSessionID = "com.mycompany.uploadsession";
NSUrlSession uploadSession;

void UploadFile_WithBackgroundTransferService(UIImage aImg)
{
  // This is just creating a file on the device that we can upload to the server
  string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  string filePath = Path.Combine(path, "out.txt");
  File.WriteAllText(filePath, "Howdy, world.");

  NSUrlSessionConfiguration sessionConf = NSUrlSessionConfiguration.BackgroundSessionConfiguration(uploadSessionID);
  if (uploadSession == null)
    uploadSession = NSUrlSession.FromConfiguration(sessionConf, new UploadDelegate(), new NSOperationQueue());

  NSUrl uploadHandlerUrl = new NSUrl("http://UrlToMyUploadHandler", false);
  NSUrlRequest request = NSUrlRequest.FromUrl(uploadHandlerUrl);

  NSUrl fileUrl = new NSUrl(filePath, false);
  NSUrlSessionUploadTask uploadTask1 = uploadSession.CreateUploadTask(request, fileUrl);
  uploadTask1.Resume();
}

private class UploadDelegate : NSUrlSessionTaskDelegate
{
  public UploadDelegate() { }

  public override void DidSendBodyData(NSUrlSession session, NSUrlSessionTask task, long bytesSent, long totalBytesSent, long totalBytesExpectedToSend)
  {
    Console.WriteLine("BgTransfer " + sessionIdentifier + " - DidSendBodyData");
  }

  public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
  {
    // Error is being caught here:
    // "Error Domain=kCFErrorDomainCFNetwork Code=303 \"The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)\" UserInfo=0x18151500 {NSErrorFailingURLKey=http://blahblah.com, NSErrorFailingURLStringKey=http://blahblah.com"
  }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>