Solved

File Upload ASP.Net C#

Posted on 2010-11-17
8
1,055 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
[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
  • 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
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.

 
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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