Solved

Save uploaded files in a folder placed under Solution Explorer

Posted on 2008-10-28
23
204 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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