Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File Upload ASP.Net C#

Posted on 2010-11-17
8
Medium Priority
?
1,060 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 1000 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 500 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 500 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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