How to get request content in ASP.Net

I have the following code that sends a file to an ASP.net page:

using (var Client = new WebClient()) Client.UploadFile("http://localhost:1625/Upload.aspx", @"TestFile.csv"); 

On the server page, I read the contents of the request:

  var Contents = new byte[Request.InputStream.Length]; Request.InputStream.Read(Contents, 0, (int)Request.InputStream.Length); 

This is what I get in the content:

 -----------------------8cf2bdc76fe2b40 Content-Disposition: form-data; name="file"; filename="TestFile.csv" Content-Type: application/octet-stream 1;Test 1 2;Test 2 3;Test 3 4;Test 4 5;Test 5 -----------------------8cf2bdc76fe2b40-- 

The actual contents of the file are just 1;Test 1 ... 5;Test 5 . My question is how to get only the contents of the file, and not the entire request with headers?

+4
source share
4 answers

Try this, first upload the file:

  { HttpFileCollection files = Request.Files; HttpPostedFile file = files[0]; int filelength = file.ContentLength; byte[] input = new byte[filelength ]; file.InputStream.Read(input, 0, filelength ); } 
+1
source

Try working with the HttpRequest class . It has the Files property, which says that files downloaded by the client are stored. I have not tested this ...

0
source

you can use NameValueCollection to get the contents of the request in ASP.Net:

 NameValueCollection nvc = Request.Form; string firstname, lastname; if (!string.IsNullOrEmpty(nvc["txtFirstName"])) { firstname = nvc["txtFirstName"]; } if (!string.IsNullOrEmpty(nvc["txtLastName"])) { lastname = nvc["txtLastName"]; } 

here txtFirstName and txtLastName are both the Control Names of the previous page.

0
source

Try

 var Content = Client.UploadFile("http://localhost:1625/Upload.aspx", @"TestFile.csv"); string fileContents = Encoding.ASCII.GetString(Contents)); 
0
source

All Articles