Solved

Save uploaded files in a folder placed under Solution Explorer

Posted on 2008-10-28
23
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio npm 1 39
Set arraylist as datatable 2 29
.NET universe documentation poster 2 25
Creating a Windows C++ program in visual studio 2015 5 31
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

751 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