Kosty_Fomin

Untitled

May 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace kursovaja
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.  
  26.         }
  27.  
  28.         private void openToolStripMenuItem1_Click(object sender, EventArgs e)
  29.         {
  30.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  31.             openFileDialog1.Filter = "CSV files (.csv)|*.csv";
  32.             openFileDialog1.FilterIndex = 1;
  33.  
  34.             if (openFileDialog1.ShowDialog() == DialogResult.OK) // Test result.
  35.             {
  36.                 try
  37.                 {
  38.                     string file = openFileDialog1.FileName;//reads path to file
  39.  
  40.                     NameValueCollection nvc = new NameValueCollection();
  41.                     HttpUploadFile("http://192.168.1.48:5001",
  42.                          file, "file", "file/csv", nvc);
  43.  
  44.  
  45.                   }
  46.  
  47.                 catch (Exception ex)
  48.                 {
  49.                     MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
  50.                 }
  51.             }
  52.         }
  53.  
  54.  
  55.  
  56.  
  57.         public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
  58.         {
  59.             Console.WriteLine(string.Format("Uploading {0} to {1}", file, url));
  60.             string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  61.             byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  62.  
  63.             HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  64.             wr.ContentType = "multipart/form-data; boundary=" + boundary;
  65.             wr.Method = "POST";
  66.             wr.KeepAlive = true;
  67.             wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  68.  
  69.             Stream rs = wr.GetRequestStream();
  70.  
  71.             string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  72.             foreach (string key in nvc.Keys)
  73.             {
  74.                 rs.Write(boundarybytes, 0, boundarybytes.Length);
  75.                 string formitem = string.Format(formdataTemplate, key, nvc[key]);
  76.                 byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  77.                 rs.Write(formitembytes, 0, formitembytes.Length);
  78.             }
  79.             rs.Write(boundarybytes, 0, boundarybytes.Length);
  80.  
  81.             string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  82.             string header = string.Format(headerTemplate, paramName, file, contentType);
  83.             byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  84.             rs.Write(headerbytes, 0, headerbytes.Length);
  85.  
  86.             FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
  87.             byte[] buffer = new byte[4096];
  88.             int bytesRead = 0;
  89.             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  90.             {
  91.                 rs.Write(buffer, 0, bytesRead);
  92.             }
  93.             fileStream.Close();
  94.  
  95.             byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  96.             rs.Write(trailer, 0, trailer.Length);
  97.             rs.Close();
  98.  
  99.             WebResponse wresp = null;
  100.             try
  101.             {
  102.                 wresp = wr.GetResponse();
  103.                 Stream stream2 = wresp.GetResponseStream();
  104.                 StreamReader reader2 = new StreamReader(stream2);
  105.                 Console.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 Console.WriteLine("Error uploading file", ex);
  110.                 if (wresp != null)
  111.                 {
  112.                     wresp.Close();
  113.                     wresp = null;
  114.                 }
  115.             }
  116.             finally
  117.             {
  118.                 wr = null;
  119.             }
  120.         }
  121.  
  122.  
  123.     } }
Advertisement
Add Comment
Please, Sign In to add comment