file upload

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

file upload

 
0
  #1
Dec 28th, 2008
Hi I am trying to upload an excel file to the server.
But it won't work, every time I am trying to upload an xls file and press the button. it writes: No file was uploaded.

as if i had no file. you are welcome to try on: http://radio.web.surftown.dk/admin/indset_program.aspx
the top section where you can upload a file from your computer

my code looks like this:

  1. protected void Button2_Click(object sender, EventArgs e)
  2. {
  3. string sSavePath;
  4.  
  5. sSavePath = "../";
  6.  
  7. if (FileUpload.PostedFile != null)
  8. {
  9. // Check file size (mustn’t be 0)
  10. HttpPostedFile myFile = FileUpload.PostedFile;
  11. int nFileLen = myFile.ContentLength;
  12. if (nFileLen == 0)
  13. {
  14. lblOutput_excel.Text = "No file was uploaded.";
  15. return;
  16. }
  17.  
  18.  
  19. if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".xls")
  20. {
  21. lblOutput.Text = "The file must have an extension of xls";
  22. return;
  23.  
  24. }
  25.  
  26. byte[] myData = new Byte[nFileLen];
  27. myFile.InputStream.Read(myData, 0, nFileLen);
  28.  
  29. System.IO.FileStream newFile
  30. = new System.IO.FileStream(Server.MapPath(sSavePath),
  31. System.IO.FileMode.Create);
  32. newFile.Write(myData, 0, myData.Length);
  33. newFile.Close();
  34.  
  35. }
  36.  
  37.  
  38.  
  39. }
Hope someone can help?
Last edited by peter_budo; Dec 29th, 2008 at 11:32 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: file upload

 
0
  #2
Dec 28th, 2008
FileUploader control does not work in the upload panel. In other words, it isn't Ajax compatible control. You need to do a regular postback through a trigger. It's a security constraint in the design of the component. You can look it up on msdn.
Last edited by iDeveloper; Dec 28th, 2008 at 4:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Re: file upload

 
0
  #3
Dec 28th, 2008
I am not using Ajax. I using an ordinary button. So isn't that a regular postback?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: file upload

 
0
  #4
Dec 28th, 2008
Okay, i just saw a reference to Update Panel in your page code, but your upload control is declared outside of it. Use breakpoints and run your page in debug mode. Set break points inside of the first if statement and see what you're getting in FileUpload.PostedFile. Check for any internal errors and property values.

Also, don't use system.io to save the file. You can do so with FileUpload.PostedFile.SaveAs();
Last edited by iDeveloper; Dec 28th, 2008 at 8:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Re: file upload

 
0
  #5
Dec 29th, 2008
Ok I put some breakpoints at:
if (FileUpload.PostedFile != null)
and
int nFileLen = myFile.ContentLength;
And when i move the curser over:
int nFileLen = myFile.ContentLength;
it writes: myFile.ContentLength 0

does that meen that it thinks that the file doesn't fill more than 0?
and how do I change that?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: file upload

 
0
  #6
Dec 29th, 2008
Originally Posted by kischi View Post
Ok I put some breakpoints at:
if (FileUpload.PostedFile != null)
and
int nFileLen = myFile.ContentLength;
And when i move the curser over:
int nFileLen = myFile.ContentLength;
it writes: myFile.ContentLength 0

does that meen that it thinks that the file doesn't fill more than 0?
and how do I change that?
Open your web.config. Look at the <authentication> tag and check the mode. Is it set to "Forms"? If so, change it to "Windows". This *should* work. Note, however, that anybody running that page will run as aspnet user. You can use impersonation to impersonate an account with less privileges. Now, don't judge me on this but this probably makes sense since you're uploading data to the server, i'd imagine you have to be able to do this as a privileged user, hence the windows authentication mode. Hope that helps.
Last edited by iDeveloper; Dec 29th, 2008 at 10:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: ruwanthaka is an unknown quantity at this point 
Solved Threads: 1
ruwanthaka ruwanthaka is offline Offline
Newbie Poster

Re: file upload

 
0
  #7
Dec 29th, 2008
//use System.Web.UI.HtmlControls.HtmlInputFile >> filSelectFile
string nameOfFile = filSelectFile.Value;//Get selected file
//Get actual file name
int last = nameOfFile.LastIndexOf("\\");
nameOfFile = nameOfFile.Substring(last + 1);
//Saving location
//this must be virtual folder of your web server
string uploadFolder = ConfigurationSettings.AppSettings["FileUploadFolder"];
nameOfFile = uploadFolder +"\\" + nameOfFile;
//Upload the file
filSelectFile.PostedFile.SaveAs(nameOfFile);
Last edited by ruwanthaka; Dec 29th, 2008 at 11:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Re: file upload

 
0
  #8
Dec 30th, 2008
The autentication was allready set to Windows.

//use System.Web.UI.HtmlControls.HtmlInputFile >> filSelectFile
string nameOfFile = filSelectFile.Value;//Get selected file
//Get actual file name
int last = nameOfFile.LastIndexOf("\\");
nameOfFile = nameOfFile.Substring(last + 1);
//Saving location
//this must be virtual folder of your web server
string uploadFolder = ConfigurationSettings.AppSettings["FileUploadFolder"];
nameOfFile = uploadFolder +"\\" + nameOfFile;
//Upload the file
filSelectFile.PostedFile.SaveAs(nameOfFile);
Where would I put this into the code. Or is this the only code that should be on the page instead of my other code?

Thanks
Last edited by kischi; Dec 30th, 2008 at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Re: file upload

 
0
  #9
Dec 30th, 2008
I got it to work now, it was just a stupid typo. Sorry I didn't see it earlier.

But thank a mill for your help. :-D
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC