0

我正在使用 ajax AsyncFileUpload 控件。我有要求,当用户单击文件上传器并从图片中选择文件时,图片将立即显示在我的图像控件上我使用以下方法

  protected void fileUploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {    
string imagepath = Server.MapPath("~/Pics/");
            string serverfilename = Path.GetFileName(AsyncFileUpload1.PostedFile.FileName);
            string fullpath = Path.Combine(imagepath, serverfilename);
            string path = "~/Pics/" + serverfilename;
            //string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
            //string path = Server.MapPath("~/Pics/") + filename;

            AsyncFileUpload1.SaveAs(fullpath);
            //  FileUpload1.PostedFile.SaveAs(path);
            SqlCommand cmd = new SqlCommand("insert findfriend values('" + path + "','" + TextBox1.Text + "')", con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            SqlCommand GetImage = new SqlCommand("select * from findfriend where name='" + TextBox1.Text + "'", con);
            GetImage.CommandType = CommandType.Text;
            SqlDataReader dr = GetImage.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
            {



                    Image1.ImageUrl = dr["picture"].ToString();


            }
}

我的aspx源

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript">
         // This function will execute after file uploaded successfully
         function uploadComplete() {
             document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File Uploaded Successfully";

         }
         // This function will execute if file upload fails
         function uploadError() {
             document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File upload Failed.";


                      }

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <ajax:ToolkitScriptManager ID="scriptManager1" runat="server"/>
        Your Name:&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server" Width="223px"></asp:TextBox>
        <br />
        <br />
        Upload Picture:<asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
         <ajax:AsyncFileUpload ID="AsyncFileUpload1" OnClientUploadComplete="uploadComplete" OnClientUploadError="uploadError"
CompleteBackColor="White" Width="350px" runat="server" UploaderStyle="Modern" UploadingBackColor="#CCFFFF"
ThrobberID="imgLoad" OnUploadedComplete="fileUploadComplete" />
        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
        <br />
        <br />

        <asp:Image ID="Image1" runat="server" Height="295px" Width="338px" />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
        &nbsp;<asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            Text="Button" />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>

    </div>
    </form>
</body>
</html>

现在的问题是我的图片路径已成功保存到数据库中图片未与图片控件绑定专家请告诉我我错在哪里

4

1 回答 1

0

这是因为您正在尝试访问完整路径,即从根级别访问路径,如 C:/Folder1/Folder2; 这在网站的情况下不起作用。

我假设解决方案文件夹中的 Pic 文件夹。

所以路径必须是这样的

  Image1.ImageUrl = @"Pic\logo.jpg";

Pic\logo.jpg" 

是来自您的数据库的值

您不需要使用绝对路径 ~

于 2013-07-10T10:07:46.867 回答