Reputation: 5144
I have the following code which posts file to 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 contents:
-----------------------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--
Actual file contents are just 1;Test 1 ... 5;Test 5
.
My question is how to get only file contents and not the whole request with headers?
Upvotes: 4
Views: 6222
Reputation: 10044
Try
var Content = Client.UploadFile("http://localhost:1625/Upload.aspx", @"TestFile.csv");
string fileContents = Encoding.ASCII.GetString(Contents));
Upvotes: 0
Reputation: 5483
Try this, get the posted file first:
{
HttpFileCollection files = Request.Files;
HttpPostedFile file = files[0];
int filelength = file.ContentLength;
byte[] input = new byte[filelength ];
file.InputStream.Read(input, 0, filelength );
}
Upvotes: 1
Reputation: 1032
Try to work with the HttpRequest class. It has a property Files which states holding the files uploaded by the client. I did not test this...
Upvotes: 0
Reputation: 4431
you can use NameValueCollection to getting request content 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
both are the Names of the controls of previous page.
Upvotes: 0