2

Im Not sure what this is telling me? I have this in place:

<system.web>
    <customErrors mode="Off"/>

When I click on a button that it is supposed to upload a file. I get the error Listed above when doing so. I am running on my local machine. I try to debug it and I get the error and not my break point. I put the "hello" line in thinking it was trying to evaluate the "if" statement before doing my breakpoint. Still fails and not sure why.

 protected void btnUpload_Click(object sender, EventArgs e)
 {
     string a = "hello";
     if (FuQuote.HasFile)
     {
         string path = "~/Quotes/" + FuQuote.FileName;
         FuQuote.SaveAs(MapPath(path));
     }
 } 

This problem can be caused by a variety of issues, including:

  • Internet connectivity has been lost.
  • The website is temporarily unavailable.
  • The Domain Name Server (DNS) is not reachable.
  • The Domain Name Server (DNS) does not have a listing for the website's domain.
  • There might be a typing error in the address.
  • If this is an HTTPS (secure) address, click Tools, click Internet Options, click Advanced, and check to be sure the SSL and TLS protocols are enabled under the security section.
brenjt
  • 15,997
  • 13
  • 77
  • 118
StephanM
  • 733
  • 3
  • 22
  • 36
  • 1
    I just figured out that it has something to do with the FileUpload control. If I just click on the button for the code I posted with out giving it a file I hit my break point. if I use the control and pick a file then press the button it fails. – StephanM Oct 21 '11 at 17:59
  • What is the output of `FuQuote.FileName`? If my memory serves me correctly, doesn't that return the full path to the file? – James Johnson Oct 21 '11 at 18:36
  • yes you are correct it puts the full pathname in the text bax that you see. but it doesn't do the actual upload. the code I have in the button is suppose to do that. But it seems to fail calling the function. – StephanM Oct 21 '11 at 18:51
  • I just determine it has something to do with the upload control. It fails when I get a file from a directory that is not the same as the one it first opened up to. Meaning if I change directories at all it fails. – StephanM Oct 21 '11 at 18:58
  • That doesn't sound right to me. Have you tried with my suggestion? – James Johnson Oct 21 '11 at 19:00
  • When it works it only has the name of the FuQuote.FileName = the name of the file with no path information – StephanM Oct 21 '11 at 20:13

3 Answers3

4

What is your maxRequestLength value set to in your web.config? You are probably selecting a file that is bigger than the maxRequestLength value.

<system.web>
    <httpRuntime maxRequestLength="4096"/>
</system.web>
mccrager
  • 1,038
  • 6
  • 13
  • Fixed the name of the property, I meant maxRequestLength, not httpRequest. maxRequestLength defaults to 4096kb (4mb) – mccrager Oct 21 '11 at 18:03
  • I currently have no setting for that. – StephanM Oct 21 '11 at 18:49
  • See this post also http://stackoverflow.com/questions/7804622/how-to-upload-content-more-than-2-mbs-on-website-created-using-asp-net-4-0/7804670#7804670 – Prasanth Oct 21 '11 at 18:54
  • Ok, I don't know how this makes sence in anyones mind, but it was the maxRequestedLength I doubled it and it worked, kind of. Why would it give the error it does at the call of the function to upload it, before you took any action against the control. Thank you all for your help – StephanM Oct 21 '11 at 20:49
  • The control should look at the file size first and immediately will cause that error if the file size is over the request length that is set. Mark my suggestion as an answer please! – mccrager Oct 24 '11 at 18:59
2

The max request length is creating trouble for you. By default the maximum allowed file upload size is 4MB. If you try to upload a file with higher size the connection will reset and won't reach the "HasFile" code. Check the size of the file you tried uploading and try with a smaller file. You can increase the file size limit by adding

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="SIZE" />
  </system.web>
</configuration>

The tags and will be there by default. If it is there in web.config add the line to it. SIZE should be replaced with the size limit.

NOTE : size is entered in KB.

Also, its always better to write such a code inside a try-catch block. There are n-number of possibilities for an exception to occur.

protected void btnUpload_Click(object sender, EventArgs e)
 {
     try
     {
         string a = "hello";
         if (FuQuote.HasFile)
         {
             string path = "~/Quotes/" + FuQuote.FileName;
             FuQuote.SaveAs(MapPath(path));
         }
     }catch(Exception ex)
     {
         // Exception handling code goes here.
     }
 } 
Robin
  • 2,339
  • 1
  • 15
  • 11
0

The FileName property of the upload control returns the full path to the file. You need to parse the file name as part of your upload logic. I think you need to use FuQuote.PostedFile.FileName too.

Import the System.IO namespace and do this:

string path = String.Format("~/Quotes/{0}", Path.GetFileName(FuQuote.PostedFile.FileName));

FuQuote.SaveAs(Server.MapPath(path)); 
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Thats all well and good, but I do not believe I am getting to that point. If I change Directories in the Upload Control then the function call fails when I click the button. So I nefer get to the point of being able to see if FuQuote.HasFile = true – StephanM Oct 21 '11 at 19:19
  • I've done many file uploads before and I've never encountered that problem, so I'm tempted to think that there is an issue in your code somewhere, or on the client you're uploading the file from. – James Johnson Oct 21 '11 at 19:21
  • It's all on the same machine VS2010 development. its almost like it is getting lost when i search for a file to upload. If I don't search it works. almost like it might be changing the directory structure under the covers for the whole web page and it gets lost. – StephanM Oct 21 '11 at 19:26