Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An example on how to upload a file to a REST service is shown below:</p> <pre><code>private byte[] UseWebClientForFileUpload(string serviceBaseUrl, String resourceUrl, string filePath) { var c = new WebClient(); c.OpenWrite(string.Concat(serviceBaseUrl, resourceUrl), "POST"); c.Headers[HttpRequestHeader.ContentType] = "application/octet-stream"; return c.UploadFile(string.Concat(serviceBaseUrl, resourceUrl), filePath); } </code></pre> <p>Make sure to set appropriate content type. I think we cannot pass multiple parameters when using Stream as one of the params, so in order to get the filename and the stream just pass everything as a single stream and then use a parser that would seperate your stream. There is something called a multipartParser as shown below: </p> <pre><code>public class MultipartParser { public MultipartParser(Stream stream) { this.Parse(stream, Encoding.UTF8); ParseParameter(stream, Encoding.UTF8); } public MultipartParser(Stream stream, Encoding encoding) { this.Parse(stream, encoding); } private void Parse(Stream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex &gt; -1) { string delimiter = content.Substring(0, content.IndexOf("\r\n")); // Look for Content-Type Regex re = new Regex(@"(?&lt;=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(content); // Look for filename re = new Regex(@"(?&lt;=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(content); // Did we find the required values? if (contentTypeMatch.Success &amp;&amp; filenameMatch.Success) { // Set properties this.ContentType = contentTypeMatch.Value.Trim(); this.Filename = filenameMatch.Value.Trim(); // Get the start &amp; end indexes of the file contents int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter); int endIndex = IndexOf(data, delimiterBytes, startIndex); int contentLength = endIndex - startIndex; // Extract the file contents from the byte array byte[] fileData = new byte[contentLength]; Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); this.FileContents = fileData; this.Success = true; } } } private void ParseParameter(Stream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex &gt; -1) { string delimiter = content.Substring(0, content.IndexOf("\r\n")); string[] splitContents = content.Split(new[] {delimiter}, StringSplitOptions.RemoveEmptyEntries); foreach (string t in splitContents) { // Look for Content-Type Regex contentTypeRegex = new Regex(@"(?&lt;=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = contentTypeRegex.Match(t); // Look for name of parameter Regex re = new Regex(@"(?&lt;=name\=\"")(.*)"); Match name = re.Match(t); // Look for filename re = new Regex(@"(?&lt;=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(t); // Did we find the required values? if (name.Success || filenameMatch.Success) { // Set properties //this.ContentType = name.Value.Trim(); int startIndex; if (filenameMatch.Success) { this.Filename = filenameMatch.Value.Trim(); } if(contentTypeMatch.Success) { // Get the start &amp; end indexes of the file contents startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; } else { startIndex = name.Index + name.Length + "\r\n\r\n".Length; } //byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter); //int endIndex = IndexOf(data, delimiterBytes, startIndex); //int contentLength = t.Length - startIndex; string propertyData = t.Substring(startIndex - 1, t.Length - startIndex); // Extract the file contents from the byte array //byte[] paramData = new byte[contentLength]; //Buffer.BlockCopy(data, startIndex, paramData, 0, contentLength); MyContent myContent = new MyContent(); myContent.Data = encoding.GetBytes(propertyData); myContent.StringData = propertyData; myContent.PropertyName = name.Value.Trim(); if (MyContents == null) MyContents = new List&lt;MyContent&gt;(); MyContents.Add(myContent); this.Success = true; } } } } private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex) { int index = 0; int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex); if (startPos != -1) { while ((startPos + index) &lt; searchWithin.Length) { if (searchWithin[startPos + index] == serachFor[index]) { index++; if (index == serachFor.Length) { return startPos; } } else { startPos = Array.IndexOf&lt;byte&gt;(searchWithin, serachFor[0], startPos + index); if (startPos == -1) { return -1; } index = 0; } } } return -1; } private byte[] ToByteArray(Stream stream) { byte[] buffer = new byte[32768]; using (MemoryStream ms = new MemoryStream()) { while (true) { int read = stream.Read(buffer, 0, buffer.Length); if (read &lt;= 0) return ms.ToArray(); ms.Write(buffer, 0, read); } } } public List&lt;MyContent&gt; MyContents { get; set; } public bool Success { get; private set; } public string ContentType { get; private set; } public string Filename { get; private set; } public byte[] FileContents { get; private set; } } public class MyContent { public byte[] Data { get; set; } public string PropertyName { get; set; } public string StringData { get; set; } } </code></pre> <p>I did get the multipartParser from a <a href="https://bitbucket.org/lorenzopolidori/http-form-parser" rel="nofollow">here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload