943,903 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 2119
  • ASP.NET RSS
Apr 13th, 2009
0

file upload

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kischi is offline Offline
62 posts
since Dec 2008
Apr 13th, 2009
0

Re: file upload

HI,
Add fileupload control in your page. Then
c# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 13th, 2009
0

Re: file upload

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:

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kischi is offline Offline
62 posts
since Dec 2008
Apr 13th, 2009
0

Re: file upload

Replace filename by
ASP.NET Syntax (Toggle Plain Text)
  1. file.PostedFile.FileName

ALSO REPLACE file.ContentType BY
ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 13th, 2009
0

Re: file upload

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:

ASP.NET Syntax (Toggle Plain Text)
  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:

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kischi is offline Offline
62 posts
since Dec 2008
Apr 13th, 2009
0

Re: file upload

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
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: login+ Contol panel.aspx
Next Thread in ASP.NET Forum Timeline: Textbox is not losing its previous value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC