public HttpResponseMessage UploadDocument() { try { string url = HttpContext.Current.Request.Form["url"]; // get documents storage path string documentStoragePath = GlobalConfiguration.GetConversionConfiguration().GetFilesDirectory(); bool rewrite = bool.Parse(HttpContext.Current.Request.Form["rewrite"]); string fileSavePath = ""; if (string.IsNullOrEmpty(url)) { if (HttpContext.Current.Request.Files.AllKeys != null) { // Get the uploaded document from the Files collection var httpPostedFile = HttpContext.Current.Request.Files["file"]; if (httpPostedFile != null) { if (rewrite) { // Get the complete file path fileSavePath = Path.Combine(documentStoragePath, httpPostedFile.FileName); } else { fileSavePath = Resources.GetFreeFileName(documentStoragePath, httpPostedFile.FileName); } // Save the uploaded file to "UploadedFiles" folder httpPostedFile.SaveAs(fileSavePath); } } } else { using (WebClient client = new WebClient()) { // get file name from the URL Uri uri = new Uri(url); string fileName = Path.GetFileName(uri.LocalPath); if (rewrite) { // Get the complete file path fileSavePath = Path.Combine(documentStoragePath, fileName); } else { fileSavePath = Resources.GetFreeFileName(documentStoragePath, fileName); } // Download the Web resource and save it into the current filesystem folder. client.DownloadFile(url, fileSavePath); } } UploadedDocumentEntity uploadedDocument = new UploadedDocumentEntity(); uploadedDocument.guid = fileSavePath; return(Request.CreateResponse(HttpStatusCode.OK, uploadedDocument)); } catch (System.Exception ex) { // set exception message return(Request.CreateResponse(HttpStatusCode.InternalServerError, Resources.GenerateException(ex))); } }