我正在使用 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:
<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" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Button" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
现在的问题是我的图片路径已成功保存到数据库中图片未与图片控件绑定专家请告诉我我错在哪里