Question about uploading file to asp script

P

Piotrekk

Hi

My application is uploading the file to the ASP script. I am dealing
with situation where ASP script is not interested in receiving a file.
I simulate this by returning from ASP script:

protected void Page_Load(object sender, EventArgs e)
{
return
}

Then I am having difficulties in detecting such a situation on the
client side. The problem is that client is writing the file into the
buffer anyways. I tried closing Request.InputStream. Would appreciate
any help. Here is the client side code:

req = WebRequest.Create(url);
req.Method = "POST";

//Headers
req.ContentType = "multipart/form-data";
req.ContentLength = fileSize;
req.Headers.Add("Name", file.Name);
req.Headers.Add("Path", path);
req.Headers.Add("SessionID", session);
req.Timeout = int.MaxValue;

Stream stream = req.GetRequestStream();

using (BinaryWriter writer = new BinaryWriter(stream))
{
FileStream fs = new FileStream(file.FullName,
FileMode.Open);

using (BinaryReader reader = new BinaryReader(fs))
{
int i = 0;
long total = 0;

byte[] buffer = new byte[32768];

while (((i = reader.Read(buffer, 0,
buffer.Length)) > 0) && !Stop)
{
writer.Write(buffer, 0, i);
total += i;
uploadWorker.ReportProgress((int)(total *
100 / fileSize));
}
}
}

Kind Regards
PK
 
A

Anthony Jones

Piotrekk said:
Hi

My application is uploading the file to the ASP script. I am dealing
with situation where ASP script is not interested in receiving a file.
I simulate this by returning from ASP script:

protected void Page_Load(object sender, EventArgs e)
{
return
}

Then I am having difficulties in detecting such a situation on the
client side. The problem is that client is writing the file into the
buffer anyways. I tried closing Request.InputStream. Would appreciate
any help. Here is the client side code:

req = WebRequest.Create(url);
req.Method = "POST";

//Headers
req.ContentType = "multipart/form-data";
req.ContentLength = fileSize;
req.Headers.Add("Name", file.Name);
req.Headers.Add("Path", path);
req.Headers.Add("SessionID", session);
req.Timeout = int.MaxValue;

Stream stream = req.GetRequestStream();

using (BinaryWriter writer = new BinaryWriter(stream))
{
FileStream fs = new FileStream(file.FullName,
FileMode.Open);

using (BinaryReader reader = new BinaryReader(fs))
{
int i = 0;
long total = 0;

byte[] buffer = new byte[32768];

while (((i = reader.Read(buffer, 0,
buffer.Length)) > 0) && !Stop)
{
writer.Write(buffer, 0, i);
total += i;
uploadWorker.ReportProgress((int)(total *
100 / fileSize));
}
}
}

Not really a C# question.

What is unclear is whether the client code is expecting or can be modified
to expect an exception as a result of the server rejecting the POST.

You need to be sending back a non-200 response status to the client.
Probably the best match would be 405 Method Not Allowed. That should cause
the HttpRequest or the stream it returns to thrown an exception. Belts and
braces you can close the server-side to the Request connection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top