file upload

Please support our ASP.NET advertiser: Intel Parallel Studio Home
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
Apr 13th, 2009
Hi I'm trying to make a file upload function for pictures.
So can anyone please help me to make a simple file upload function in c#?

I've tried to make one, but it won't seem to work, so I've decided to start over, just with a simple file upload funktion.

Hope someone can help?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: file upload

 
0
  #2
Apr 13th, 2009
HI,
Add fileupload control in your page. Then
  1. if (null != file.PostedFile)
  2. {
  3. try
  4. {
  5. if (file.ContentType.IndexOf("excel") >= 0)
  6. file.SaveAs(Server.MapPath(@"\Upload\Excel\" + filename));
  7. else if (file.ContentType.IndexOf("word") >= 0)
  8. file.SaveAs(Server.MapPath(@"\Upload\Word\" + filename));
  9. else
  10. file.SaveAs(Server.MapPath(@"\Upload\Other\" + filename));
  11. }
  12. catch (Exception e)
  13. {
  14.  
  15. }
  16. }
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
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
Apr 13th, 2009
Now I tryed that, but it didn't work.

Under: filename a red line appears, and if I hold the mouse on that it writes: The name 'filename' does not exist in the current context.

and under ContentType a red line also appears witch says:

'System.web.ui.webcontrols.Fileupload' does not contain a definition for 'contenttype' and no extension method 'contenttype' accepting a first argument of type
'System.web.ui.webcontrols.Fileupload' could be found (are you missing a using directive or an assembly refference?)

Do you know how to fix it? please?

my whole code on my cs page looks like this:

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. using System.Configuration;
  11. using System.Collections;
  12. using System.Web.Security;
  13. using System.Web.UI.WebControls.WebParts;
  14. using System.Web.UI.HtmlControls;
  15.  
  16.  
  17. public partial class _Default : System.Web.UI.Page
  18. {
  19.  
  20.  
  21. protected void Page_Load(object sender, EventArgs e)
  22. {
  23.  
  24. if (null != file.PostedFile)
  25. {
  26.  
  27. try
  28. {
  29.  
  30. if (file.ContentType.IndexOf("excel") >= 0)
  31.  
  32. file.SaveAs(Server.MapPath(@"\Upload\Excel\" + filename));
  33.  
  34. else if (file.ContentType.IndexOf("word") >= 0)
  35.  
  36. file.SaveAs(Server.MapPath(@"\Upload\Word\" + filename));
  37.  
  38. else
  39.  
  40. file.SaveAs(Server.MapPath(@"\Upload\Other\" + filename));
  41.  
  42. }
  43.  
  44. catch (Exception e)
  45. {
  46.  
  47. }
  48. }
  49. }
  50. }
Last edited by peter_budo; Apr 15th, 2009 at 7:57 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags. Keep It Spam-Free Do not spam, advertise, plug your website.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: file upload

 
0
  #4
Apr 13th, 2009
Replace filename by
  1. file.PostedFile.FileName

ALSO REPLACE file.ContentType BY
  1. file.PostedFile.ContentType

Also makesure that you have cretaed a folder named Upload in root & also create another 3 folders named Excel,Word,Other as subfolder.
Last edited by mail2saion; Apr 13th, 2009 at 3:23 pm. Reason: Missed few points to reply
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
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
Apr 13th, 2009
Now I've tried to test my code, but it doesn't seem to save the files in the folders iv'e created.
But it doesn't write any errors, it just doesn't save the files.

all my code now looks like this on the cs page:

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. using System.Configuration;
  11. using System.Collections;
  12. using System.Web.Security;
  13. using System.Web.UI.WebControls.WebParts;
  14. using System.Web.UI.HtmlControls;
  15.  
  16.  
  17. public partial class _Default : System.Web.UI.Page
  18. {
  19.  
  20.  
  21. protected void Page_Load(object sender, EventArgs e)
  22. {
  23.  
  24.  
  25. }
  26.  
  27.  
  28. protected void upload_Click(object sender, EventArgs e)
  29. {
  30. if (null != file.PostedFile)
  31. {
  32.  
  33. try
  34. {
  35.  
  36. if (file.PostedFile.ContentType.IndexOf("excel") >= 0)
  37.  
  38. file.SaveAs(Server.MapPath(@"\Upload\Excel\" + file.PostedFile.FileName));
  39.  
  40. else if (file.PostedFile.ContentType.IndexOf("word") >= 0)
  41.  
  42. file.SaveAs(Server.MapPath(@"\Upload\Word\" + file.PostedFile.FileName));
  43.  
  44. else
  45.  
  46. file.SaveAs(Server.MapPath(@"\Upload\Other\" + file.PostedFile.FileName));
  47.  
  48. }
  49.  
  50. catch (Exception)
  51. {
  52.  
  53. }
  54. }
  55. }
  56. }

and on the aspx page:

  1.  
  2. <asp:FileUpload ID="file" runat="server" /><br />
  3. <asp:Button ID="upload" runat="server" Text="upload" onclick="upload_Click"/>

do you know what the problem can be?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: file upload

 
0
  #6
Apr 13th, 2009
Yes the problem is iis user cant write to the upload folder. Means its permission isue. Right click on upload folder which you cretated in your root folder then click properties then click on security tab then add then Write everyone click on checkname & finaly ok.

If problem doesnot resolved yet then print the exception from catch block & post the error into this thread.

If problem resolved then remove everyone permission from upload folder & give the permission to iis default user.
Last edited by mail2saion; Apr 13th, 2009 at 5:24 pm. Reason: Missed to append few lines
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC