471,354 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Question about uploading file to asp script

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
Aug 9 '08 #1
1 1329
"Piotrekk" <Pi*************@gmail.comwrote in message
news:d9**********************************@z72g2000 hsb.googlegroups.com...
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.
--
Anthony Jones - MVP ASP/ASP.NET
Aug 10 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chamomile | last post: by
1 post views Thread by Pamela Chatterjee | last post: by
4 posts views Thread by msnews.microsoft.com | last post: by
3 posts views Thread by Dean Richardson | last post: by
5 posts views Thread by Chris | last post: by
3 posts views Thread by MichaelK | last post: by
11 posts views Thread by starter08 | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.