Link to home
Start Free TrialLog in
Avatar of KavyaVS
KavyaVS

asked on

File Upload ASP.Net C#

 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
Avatar of jasonduan
jasonduan
Flag of United States of America image

try:

FileUpload1.SaveAs(Server.MaothPath("~/" + FileUpload1.FileName));
ASKER CERTIFIED SOLUTION
Avatar of jasonduan
jasonduan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KavyaVS
KavyaVS

ASKER

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
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KavyaVS

ASKER

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

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.


                   
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial