简体   繁体   中英

ASP.net c# FileUpload & On PageLoad

I am trying to do an edit profile page..

Where on page load, the user reads data from database and its displayed.

I am using a fileupload on save button click..

 <asp:Label ID="Label17" runat="server" Text="Logo"></asp:Label>
    <div id="photo" >
    <asp:Image ID="Image1" runat="server" Height="155px" Width="173px" />
    <asp:FileUpload ID="FileUpload1" runat="server" Width="220px " />
    </div>

C#

        System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
        string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
        fname = fname + Guid.NewGuid().ToString("N") + file.Extension;
        string photo = "";
        photo = fname;
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/images/dp/" + fname));
            Image1.ImageUrl = "~/images/dp/" + fname;
        }
        else
        {
            //  saved.InnerText = "Please Select Employee Photo";
        }

On page load function:

Image1.ImageUrl = "~/images/dp/" + myReader3["s_dp"].ToString();

Well, the upload function works fine, saved into database fine , the image loads on next page load fine...

The problem is when if i click Profile save button without selecting a new file, its giving an error.. If a file is selected it works and saves in database..

But for example if i were to edit name and not do anything to fileupload area, and click save button... Error pops up...

The path is not of a legal form.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The path is not of a legal form.

Source Error: 


Line 111:        string sid;
Line 112:        sid = Request.QueryString["SID"].ToString();
Line 113:        System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
Line 114:        string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
Line 115:        fname = fname + Guid.NewGuid().ToString("N") + file.Extension;

Source File: c:\Users\User\Documents\Visual Studio 2010\WebSites\HireMe\admin\editseeker.aspx.cs    Line: 113 

Any kind of help would be appreciated, hope i stated my problem clearly..

try this, i hope this could help you:

if (FileUpload1.HasFile)
{
System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
fname = fname + Guid.NewGuid().ToString("N") + file.Extension;
string photo = "";
photo = fname;

FileUpload1.SaveAs(Server.MapPath("~/images/dp/" + fname));
Image1.ImageUrl = "~/images/dp/" + fname;
}
else
{
    //  saved.InnerText = "Please Select Employee Photo";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM