Advertisement
Guest User

FileRequestParameter

a guest
Mar 29th, 2014
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. namespace Zelka.Apis
  2. {
  3.     using System.IO;
  4.     using System.Text;
  5.  
  6.     public class FileRequestParameter : IRequestParameter
  7.     {
  8.         private readonly byte[] Content;
  9.  
  10.         public FileRequestParameter(string key, string fileLocation, string uploadFileName)
  11.         {
  12.             this.Key = key;
  13.             this.Content = File.ReadAllBytes(fileLocation);
  14.             this.UploadFileName = uploadFileName;
  15.         }
  16.  
  17.         public FileRequestParameter(string key, string fileLocation) : this(key, fileLocation, Path.GetFileName(fileLocation))
  18.         {
  19.         }
  20.  
  21.         public FileRequestParameter(string key, byte[] fileBytes, string fileName)
  22.         {
  23.             this.Key = key;
  24.             this.Content = fileBytes;
  25.             this.UploadFileName = fileName;
  26.         }
  27.        
  28.         public string UploadFileName { get; set; }
  29.  
  30.         public string ContentType { get; set; }
  31.  
  32.         public string Key { get; set; }
  33.  
  34.         public void WriteToStream(Stream stream, string boundary, Encoding encoding)
  35.         {
  36.             const string template = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n";
  37.             string header = string.Format(template, boundary, this.Key, this.UploadFileName, this.ContentType);
  38.  
  39.             stream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
  40.             stream.Write(this.Content, 0, this.Content.Length);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement