我正在使用ajax AsyncFileUpload控件。我有要求,当用户点击fileuploader并从图片中选择文件时,图片将立即显示在我的图像控件上我使用以下方法
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>
现在的问题是我的图片路径成功保存到数据库中图像没有绑定图像控件专家告诉我请问我哪里错了
答案 0 :(得分:0)
这是因为您尝试访问完整路径,即从根级别开始路径,如C:/ Folder1 / Folder2;对于网站,这不起作用。
我假设解决方案文件夹中有Pic文件夹。
所以路径必须像
Image1.ImageUrl = @"Pic\logo.jpg";
Pic\logo.jpg"
是来自您的数据库的值
使用〜
不需要使用绝对路径