Save uploaded files in a folder placed under Solution Explorer

Hi
I am using a upload file script which saves uploaded file in folder UploadedUserFiles located on C drive (see please code snippet). I will save uploaded files in a folder called media placed in Solution Explorer Instead .

protected void Button1_Click(object sender, EventArgs e)
    {
        string strFileName;
        strFileName = FileUpload1.PostedFile.FileName;
        string c = System.IO.Path.GetFileName(strFileName);
        try
        {
           FileUpload1.PostedFile.SaveAs("C:\\UploadedUserFiles\\" + c);
        }
        catch (Exception exp)
        {
            Span1.InnerHtml = "Some Error occured.";
        }

Open in new window

sam20Asked:
Who is Participating?
 
sm394Connect With a Mentor Commented:
have you tried that this is tested and working on my local machine without any error

if (!System.IO.Directory.Exists(Server.MapPath("/media/")))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath("/media/"));
            }
            FileUpload1.PostedFile.SaveAs(Server.MapPath("/media/") + FileUpload1.FileName);

0
 
sm394Commented:
try that
FileUpload1.PostedFile.SaveAs("/media/" + c);
0
 
TechTiger007Commented:


You can use the relative path of the folder from your page path. Here "MyFolder" is the name of the folder in the solution explorer. MyFolder is inside the folder where your file exist
FileUpload1.PostedFile.SaveAs(Server.MapPath("Folder\\" + c));

in web.config file
<add key="uploadpath" value="D:\\MyProject\\MyFolder\\" />
in your code use
FileUpload1.PostedFile.SaveAs(ConfigurationManager.AppSettings["uploadpath"] + c);
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
techExtremeCommented:
Hello, try this:

string c = System.IO.Path.GetFileName(strFileName);string strFullpath = Path.Combine(Server.MapPath("media"),c);

Now you have a valid path for your file name under your media folder placed in solution explorer.

0
 
sam20Author Commented:
Thanks for your answers

Hi sm394
Your sugestion gave me error
catch (Exception exp)
        {
            Span1.InnerHtml = "Some Error occured.";
        }

Hi TchTiger
Can you please tell me wher exactly I should put uploadpath(line bellow) in the web.config
<add key="uploadpath" value="C:\\Documents and Settings\\Sadi\\Desktop\\WebApplication2\\" />

Hi techExtrem
I tried your and it gave me error
Compiler Error Message: CS0136: A local variable named 'c' cannot be declared in this scope because it would give a different meaning to 'c', which is already used in a 'parent or current' scope to denote something else
0
 
techExtremeCommented:
Hi, I meant you to use the same variable not again declare it.
However do this now, and keep your declaration for the c variable as it is.

c = System.IO.Path.GetFileName(strFileName);
string strFullpath = Path.Combine(Server.MapPath("media"),c);
0
 
sam20Author Commented:
Hi techExtreme
It doesnt upload any file to media folder
0
 
sm394Commented:
try that
FileUpload1.PostedFile.SaveAs("/media/" + FileUpload1.PostedFile.FileName);
0
 
sm394Commented:
the code snippet above expect media folder on the root of your solution else tell us your folder structre
0
 
sm394Commented:
also pleas post the error message
0
 
sam20Author Commented:
Hi sm394
I got the same error:Some Error occured
0
 
sm394Commented:
what is your directory structure i.e is it like that  SolutionDirectory/Media
0
 
sam20Author Commented:
yes ,Solution Explorer/media
0
 
TechTiger007Commented:
uploadpath should go inside appSettings section of web.config
<configuration>
    <appSettings>
       <add key="uploadpath" value="<path of the folder>"/>
   
0
 
sam20Author Commented:
Hi TechTiger
Tere is no <appSettings> in web.config.
0
 
techExtremeCommented:
Try
FileUpload1.postedFile.SaveAs( Server.MapPath("/Solution Explorer/media"),"\" + FileUpload1.PostedFile.FileName))

Assuming your directory structure is like:    drive:/....../Solution Explorer/media


0
 
sm394Commented:
try that
if (!System.IO.Directory.Exists(Server.MapPath("/media/")))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath("/media/"));
            }

            FileUpload1.PostedFile.SaveAs(Server.MapPath("/media/") + FileUpload1.FileName);
0
 
sam20Author Commented:
Hi techExtreme
the line
FileUpload1.postedFile.SaveAs( Server.MapPath("/C:\Documents and Settings\Sadi\Desktop\WebApplication2/media"),"\" + FileUpload1.PostedFile.FileName));

got me error:FileUpload1.postedFile.SaveAs( Server.MapPath("/C:\Documents and Settings\Sadi\Desktop\WebApplication2/media"),"\" + FileUpload1.PostedFile.FileName));

I chaned to:FileUpload1.postedFile.SaveAs( Server.MapPath("/C:/Documents and Settings/Sadi/Desktop/WebApplication2/media"),"/" + FileUpload1.PostedFile.FileName));
got error: ; expected
0
 
sam20Author Commented:
Hi sm394
No error but still no any effect.
0
 
sm394Commented:
the file has been uploaded in a folder media on the root of your project
if you search for media folder u ll find one there
as i said earlier if you could tell us the directory structure for your media folder we would be able to help you better
0
 
sam20Author Commented:
Hi sm394, You are right, it works fine and uploaded the files. Thank you very much
0
 
sam20Author Commented:
Thanks
0
 
techExtremeCommented:
Hi,
I wrote Server.MapPath("/Solution Explorer/media...
but you tried Server.MapPath("/C:\Documents and Settings\Sadi\Desktop\WebApplication2....
that's y it won't work
ANyways good that you got it solved.
0
All Courses

From novice to tech pro — start learning today.