Solved

Save uploaded files in a folder placed under Solution Explorer

Posted on 2008-10-28
23
202 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
Comment Utility
try that
FileUpload1.PostedFile.SaveAs("/media/" + c);
0
 
LVL 13

Expert Comment

by:TechTiger007
Comment Utility


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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi techExtreme
It doesnt upload any file to media folder
0
 
LVL 13

Expert Comment

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

Expert Comment

by:sm394
Comment Utility
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
Comment Utility
also pleas post the error message
0
 

Author Comment

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

Expert Comment

by:sm394
Comment Utility
what is your directory structure i.e is it like that  SolutionDirectory/Media
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sam20
Comment Utility
yes ,Solution Explorer/media
0
 
LVL 13

Expert Comment

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

Author Comment

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

Expert Comment

by:techExtreme
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi sm394
No error but still no any effect.
0
 
LVL 13

Expert Comment

by:sm394
Comment Utility
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
Comment Utility
Hi sm394, You are right, it works fine and uploaded the files. Thank you very much
0
 

Author Closing Comment

by:sam20
Comment Utility
Thanks
0
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

14 Experts available now in Live!

Get 1:1 Help Now