Solved

File Upload ASP.Net C#

Posted on 2010-11-17
8
1,046 Views
Last Modified: 2013-12-17
 protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                try
                {
                    //FileUpload1.SaveAs("C:\\Temp\\" + FileUpload1.FileName);
                    FileUpload1.SaveAs("http://localhost:61747/Default.aspx" + FileUpload1.FileName);
                         Label1.Text = "File name: " +
                         FileUpload1.PostedFile.FileName + "<br>" +
                         FileUpload1.PostedFile.ContentLength + " kb<br>" +
                         "Content type: " +
                         FileUpload1.PostedFile.ContentType;
                       
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }

ERROR: The SaveAs method is configured to require a rooted path, and the path 'http://localhost:61700/Default.aspx Accounts.rdl' is not rooted.

I want to save the browsed file in the web application I am creating. Is there any way to do that?In default.aspx page, I want to save the uploaded file.

Thanks
0
Comment
Question by:KavyaVS
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34159395
try:

FileUpload1.SaveAs(Server.MaothPath("~/" + FileUpload1.FileName));
0
 
LVL 11

Accepted Solution

by:
jasonduan earned 250 total points
ID: 34159405
FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName));
0
 

Author Comment

by:KavyaVS
ID: 34159722
It is not showing any error. But, where I can see the saved file? When I see the default.aspx page it is not there.

Thanks
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:jasonduan
ID: 34159748
It is in your website's root folder (i would guess it is the same folder as Default.aspx resides) and the file name is whatever the file name you uploaded.
0
 
LVL 5

Assisted Solution

by:vivekpv10
vivekpv10 earned 125 total points
ID: 34161476
Actually u want to store it in server.Its meanigless saying "want to store it in default.aspx" .It is possible that u can save it in root folder or u can store it in particular folder.I will show u an example..
FileUpload1.SaveAs(Server.MapPath("~") + "\\" + "Downloads" + "\\" +FileUpload1.FileName).Here Downloads is the folder where i want to store my uploaded file.Downloads folder is created inside root folder.
0
 

Author Comment

by:KavyaVS
ID: 34164710
Yes,I want to save it to the same server(the web site I am creating).I tried to add Downloads folder,but I couldn't.Where can I add the folder.I right click the application folder,but it is adding asp.net folder only.

Thanks

0
 
LVL 8

Expert Comment

by:johny_bravo1
ID: 34166092
Hi KavyaVS,
What are you exactly trying to do.
With fileupload control,you get the file from the browser and save it to the folder on the server.
>>  FileUpload1.SaveAs("http://localhost:61747/Default.aspx" + FileUpload1.FileName); not the proper way,

Have you tried,
FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName));
as suggested by the experts.

With this you can see the file, in the root folder of your web application.


                   
0
 
LVL 8

Assisted Solution

by:johny_bravo1
johny_bravo1 earned 125 total points
ID: 34166142
>>I tried to add Downloads folder,but I couldn't.Where can I add the folder.I right click the application folder,but it is adding asp.net folder only

Create a folder named"Downloads"(you can set any name),in your application.
Right click the application folder,there you will get 'New Folder' option,set it a name.
Alternatively you can create a directory using code.

Hope this help.
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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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