Solved

Save uploaded files in a folder placed under Solution Explorer

Posted on 2008-10-28
23
203 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:sam20
  • 9
  • 8
  • 4
  • +1
23 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22819869
try that
FileUpload1.PostedFile.SaveAs("/media/" + c);
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22819893


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
 
LVL 12

Expert Comment

by:techExtreme
ID: 22819934
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
 

Author Comment

by:sam20
ID: 22820066
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
 
LVL 12

Expert Comment

by:techExtreme
ID: 22820087
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
 

Author Comment

by:sam20
ID: 22820133
Hi techExtreme
It doesnt upload any file to media folder
0
 
LVL 13

Expert Comment

by:sm394
ID: 22820136
try that
FileUpload1.PostedFile.SaveAs("/media/" + FileUpload1.PostedFile.FileName);
0
 
LVL 13

Expert Comment

by:sm394
ID: 22820142
the code snippet above expect media folder on the root of your solution else tell us your folder structre
0
 
LVL 13

Expert Comment

by:sm394
ID: 22820146
also pleas post the error message
0
 

Author Comment

by:sam20
ID: 22820157
Hi sm394
I got the same error:Some Error occured
0
 
LVL 13

Expert Comment

by:sm394
ID: 22820193
what is your directory structure i.e is it like that  SolutionDirectory/Media
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:sam20
ID: 22820208
yes ,Solution Explorer/media
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22820215
uploadpath should go inside appSettings section of web.config
<configuration>
    <appSettings>
       <add key="uploadpath" value="<path of the folder>"/>
   
0
 

Author Comment

by:sam20
ID: 22820247
Hi TechTiger
Tere is no <appSettings> in web.config.
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22820251
Try
FileUpload1.postedFile.SaveAs( Server.MapPath("/Solution Explorer/media"),"\" + FileUpload1.PostedFile.FileName))

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


0
 
LVL 13

Expert Comment

by:sm394
ID: 22820288
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
 

Author Comment

by:sam20
ID: 22820315
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
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22820340
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
 

Author Comment

by:sam20
ID: 22820375
Hi sm394
No error but still no any effect.
0
 
LVL 13

Expert Comment

by:sm394
ID: 22820427
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
 

Author Comment

by:sam20
ID: 22820454
Hi sm394, You are right, it works fine and uploaded the files. Thank you very much
0
 

Author Closing Comment

by:sam20
ID: 31510643
Thanks
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22820956
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now