private void MakeFileUpload(FileStream fileStream, string path, bool overwrite, UploadCallback uploadCallback, AsyncCallback <BackendlessFile> callback) { string boundary = DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); var fileName = Path.GetFileName(fileStream.Name); //You cannot get name of IsolatedStorageFileStream the normal way, it always returns [Unknown] (making it pass the checks against null) if (fileStream.GetType() == typeof(System.IO.IsolatedStorage.IsolatedStorageFileStream)) { fileName = Path.GetFileName(((System.IO.IsolatedStorage.IsolatedStorageFileStream)fileStream).Name); } var sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\""); sb.Append(fileName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("Content-Transfer-Encoding: binary"); sb.Append("\r\n"); sb.Append("\r\n"); var header = sb.ToString(); byte[] headerBytes = Encoding.UTF8.GetBytes(header); var urlStr = Backendless.URL + "/" + Backendless.AppId + "/" + Backendless.APIKey + "/files/" + EncodeURL(path) + "/" + EncodeURL(fileName); if (overwrite) { urlStr = urlStr + "?overwrite=true"; } var httpRequest = (HttpWebRequest) WebRequest.Create( new Uri( urlStr, UriKind.Absolute)); httpRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpRequest.Method = "POST"; httpRequest.Headers["KeepAlive"] = "true"; foreach (var h in HeadersManager.GetInstance().Headers) { httpRequest.Headers[h.Key] = h.Value; } try { var async = new RequestStreamAsyncState <BackendlessFile> { Callback = callback, UploadCallback = uploadCallback, HttpRequest = httpRequest, HeaderBytes = headerBytes, BoundaryBytes = boundaryBytes, FileStream = fileStream }; httpRequest.BeginGetRequestStream(RequestStreamCallback, async); } catch (System.Exception ex) { if (callback != null) { callback.ErrorHandler.Invoke(new BackendlessFault(ex.Message)); } else { throw; } } }
private void MakeFileUpload(Stream stream, string path, UploadCallback uploadCallback, AsyncCallback <BackendlessFile> callback) { string boundary = DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); var fileName = ""; try { fileName = Path.GetFileName(path); } catch (System.Exception ex) { if (callback != null) { callback.ErrorHandler.Invoke(new BackendlessFault(ex.Message)); } else { throw; } } var sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\""); sb.Append(fileName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("Content-Transfer-Encoding: binary"); sb.Append("\r\n"); sb.Append("\r\n"); string header = sb.ToString(); byte[] headerBytes = Encoding.UTF8.GetBytes(header); var httpRequest = (HttpWebRequest) WebRequest.Create( new Uri( Backendless.Url + "/" + Backendless.VersionNum + "/files/" + EncodeURL(path), UriKind.Absolute)); httpRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpRequest.Method = "POST"; httpRequest.Headers["KeepAlive"] = "true"; foreach (var h in HeadersManager.GetInstance().Headers) { httpRequest.Headers[h.Key] = h.Value; } try { var async = new RequestStreamAsyncState <BackendlessFile> { Callback = callback, UploadCallback = uploadCallback, HttpRequest = httpRequest, HeaderBytes = headerBytes, BoundaryBytes = boundaryBytes, Stream = stream }; httpRequest.BeginGetRequestStream(RequestStreamCallback, async); } catch (System.Exception ex) { if (callback != null) { callback.ErrorHandler.Invoke(new BackendlessFault(ex.Message)); } else { throw; } } }