?
Solved

Save uploaded files in a folder placed under Solution Explorer

Posted on 2008-10-28
23
Medium Priority
?
211 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 2000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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