Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


asp.net and uploading a file

baranbaran

Well, this is a good way to upload files but it will be slow in big files. I will come with another solution for that need.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Collections.Specialized;

public partial class PostSend : System.Web.UI.Page
{
 private static void UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc)
 {
 long length = 0;
 string boundary = "----------------------------" +
 DateTime.Now.Ticks.ToString("x");


 HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
 httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
 boundary;
 httpWebRequest2.Method = "POST";
 httpWebRequest2.KeepAlive = true;
 httpWebRequest2.Credentials =
 System.Net.CredentialCache.DefaultCredentials;

 Stream memStream = new System.IO.MemoryStream();

 byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
 boundary + "\r\n");


 string formdataTemplate = "\r\n--" + boundary +
 "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

 foreach (string key in nvc.Keys)
 {
 string formitem = string.Format(formdataTemplate, key, nvc[key]);
 byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
 memStream.Write(formitembytes, 0, formitembytes.Length);
 }


 memStream.Write(boundarybytes, 0, boundarybytes.Length);

 string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

 for (int i = 0; i < files.Length; i++)
 {

 string header = string.Format(headerTemplate, "file" + i, files[i]);

 byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

 memStream.Write(headerbytes, 0, headerbytes.Length);


 FileStream fileStream = new FileStream(files[i], FileMode.Open,
 FileAccess.Read);
 byte[] buffer = new byte[1024];
 
 int count;
 int bytesRead = 0;

 //while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
 //{
 int totalBytes = 0;
 while ((count = fileStream.Read(buffer, 0, 1024)) > 0)
 {
 memStream.Write(buffer, 0, bytesRead);

 }


 memStream.Write(boundarybytes, 0, boundarybytes.Length);


 fileStream.Close();
 }

 httpWebRequest2.ContentLength = memStream.Length;

 Stream requestStream = httpWebRequest2.GetRequestStream();

 memStream.Position = 0;
 byte[] tempBuffer = new byte[memStream.Length];
 memStream.Read(tempBuffer, 0, tempBuffer.Length);
 memStream.Close();
 requestStream.Write(tempBuffer, 0, tempBuffer.Length);
 requestStream.Close();


 WebResponse webResponse2 = httpWebRequest2.GetResponse();

 Stream stream2 = webResponse2.GetResponseStream();
 StreamReader reader2 = new StreamReader(stream2);


 Console.WriteLine(reader2.ReadToEnd());

 webResponse2.Close();
 httpWebRequest2 = null;
 webResponse2 = null;

 }
 protected void Page_Load(object sender, EventArgs e)
 {
 //string url = "http://mydomain.com/postReceive.aspx";
 string url = "http://localhost:11333/WebSite21/PostReceive.aspx";
 
 //string fileName = "stream.txt";
 string fileName = "sample.ts";
 string path = System.IO.Path.GetFullPath("C:\\test\\filesToSend\\");
 NameValueCollection myCol = new NameValueCollection();
 myCol.Add("fileName", fileName);
 string[] files = new string[1];
 files[0] = path + fileName;
 UploadFilesToRemoteUrl(url, files, "", myCol);
 }
}