简体   繁体   中英

Upload a file using AsyncFileUpload Control and store it

I am doing a project :

Front end - Visual Studio 2010

Technology : C#

Back end - Sql Server 2005

I am trying to upload a file using AsyncFileUpload Control and store it to "~/Image/" folder.

Script :

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" 
            onuploadedcomplete="AsyncFileUpload1_UploadedComplete"/>
    </ContentTemplate>

Code Behind :

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    if (AsyncFileUpload1.HasFile)
    {
        AsyncFileUpload1.SaveAs("~/Image/" + AsyncFileUpload1.FileName);
        Label2.Text = "Recieved " + AsyncFileUpload1.FileName + " Content Type" + AsyncFileUpload1.PostedFile.ContentType;
    }
}

But everytime its showing runtime error called :

The SaveAs method is configured to require a rooted path, and the path '~\Image\Filename.jpg' is not rooted.

May I know the error and its solution.

Thanks in advance,

Nikhil

 String path = "~/Image/" + FileUpload1.FileName;

应该是这样的:

 String path = Server.MapPath("~/Image/") + FileUpload1.FileName;

I figured it out:

table width="100%" style="font: 8pt verdana">
                <tr width="100%">
                <td width="40%">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:HiddenField ID="HiddenField1" runat="server" />
                </td>
                <td width="40%"><asp:Label ID="lblPicStatus" runat="server"></asp:Label></td>
                <td>
                <asp:Button ID="Button2" runat="server" Text="Upload" BackColor="White" 
                        BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" 
                        onclick="Button2_Click"/></td>
                </tr>
</table>

Code Behind,

protected void Button2_Click(object sender, EventArgs e)
{

    try
    {
        con.Open();
        if (FileUpload1.HasFile)
        {
            String fileExt = Path.GetExtension(FileUpload1.FileName);
            if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
            {
                String path = "~/Image/" + FileUpload1.FileName;
                cmd.CommandText = "update " + HttpContext.Current.User.Identity.Name + " set image = '" + path + "'";
                cmd.Connection = con;
                cmd.ExecuteNonQuery();
                FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                lblPicStatus.Text = "File to be uploaded is not an image";
            }
            con.Close();
        }
    }

    catch (Exception a)
    {
        Response.Write(a.Message);
    }
}

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