Solved

File Upload ASP.Net C#

Posted on 2010-11-17
8
1,048 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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