Solved

File Upload ASP.Net C#

Posted on 2010-11-17
8
1,040 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now