Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Zelka.Apis
- {
- using System.IO;
- using System.Text;
- public class FileRequestParameter : IRequestParameter
- {
- private readonly byte[] Content;
- public FileRequestParameter(string key, string fileLocation, string uploadFileName)
- {
- this.Key = key;
- this.Content = File.ReadAllBytes(fileLocation);
- this.UploadFileName = uploadFileName;
- }
- public FileRequestParameter(string key, string fileLocation) : this(key, fileLocation, Path.GetFileName(fileLocation))
- {
- }
- public FileRequestParameter(string key, byte[] fileBytes, string fileName)
- {
- this.Key = key;
- this.Content = fileBytes;
- this.UploadFileName = fileName;
- }
- public string UploadFileName { get; set; }
- public string ContentType { get; set; }
- public string Key { get; set; }
- public void WriteToStream(Stream stream, string boundary, Encoding encoding)
- {
- const string template = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n";
- string header = string.Format(template, boundary, this.Key, this.UploadFileName, this.ContentType);
- stream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
- stream.Write(this.Content, 0, this.Content.Length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement