这是我的上传代码 if (FileUpload1.HasFile)
{
string name = FileUpload1.FileName;//获取文件名
string type = name.Substring(name.LastIndexOf(".") + 1);//获取文件类型
string ipath = Server.MapPath("Images/"+name); //获取文件路径
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{
SqlConnection con = ZC.ReturnConnection();
SqlCommand com = new SqlCommand("Insert_Rents", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@Rent_Names", SqlDbType.VarChar, 50).Value =TextBox1.Text;
com.Parameters.Add("@Rent_Images", SqlDbType.VarChar, 50).Value = ipath;
con.Open();
com.ExecuteNonQuery();
con.Close();
FileUpload1.SaveAs(ipath); //服务器保存路径
Response.Write("<script>alert('提交成功!')</script>");
}
这是我绑定图片的代码:<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="7" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Rent_Images") %>' runat="server" />
<br />
<asp:Label ID="Rent_NamesLabel" runat="server" Text='<%# Eval("Rent_Names") %>'>
</asp:Label><br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ManitoConnectionString4 %>"
SelectCommand="SELECT [Rent_Names], [Rent_Images] FROM [Rent_Image]"></asp:SqlDataSource>
现在可能是路径问题,图片显示不出来,怎么搞
{
string name = FileUpload1.FileName;//获取文件名
string type = name.Substring(name.LastIndexOf(".") + 1);//获取文件类型
string ipath = Server.MapPath("Images/"+name); //获取文件路径
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{
SqlConnection con = ZC.ReturnConnection();
SqlCommand com = new SqlCommand("Insert_Rents", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@Rent_Names", SqlDbType.VarChar, 50).Value =TextBox1.Text;
com.Parameters.Add("@Rent_Images", SqlDbType.VarChar, 50).Value = ipath;
con.Open();
com.ExecuteNonQuery();
con.Close();
FileUpload1.SaveAs(ipath); //服务器保存路径
Response.Write("<script>alert('提交成功!')</script>");
}
这是我绑定图片的代码:<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="7" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Rent_Images") %>' runat="server" />
<br />
<asp:Label ID="Rent_NamesLabel" runat="server" Text='<%# Eval("Rent_Names") %>'>
</asp:Label><br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ManitoConnectionString4 %>"
SelectCommand="SELECT [Rent_Names], [Rent_Images] FROM [Rent_Image]"></asp:SqlDataSource>
现在可能是路径问题,图片显示不出来,怎么搞
Image1" src="D:\%e5%bc%a0%e5%85%8b%e9%be%99\WebSite7\WebSite7\WebSite7\lxwm.jpg" style="border-width:0px;" />
如果全部是放在D:\张克龙\WebSite7\WebSite7\WebSite7\Images
直接只存个文件名就行了
到时候 ~/images/qq.jpg.这样拼起来。
不过需要注意的是,上传图片的时候,存放的位置也需要是相对,而不是直接写
存放地址。 用Server.MapPath(".") 取到当前的位置。(一般是程序文件夹目录)
string ipath = Server.MapPath("Images/"+name); //获取文件路径修改为:string ipath = "../Images/"+name ; //获取文件路径
还是不行
上不是通过上传获取的?
例如: string ipath = "../Images/"+name 其实解析到客户端就是 C:\Inetpub\wwwroot\\项目名称\\Images/"+name 你的图片是不是真的在那个地址??
你也可以通过查看html页图片上,点击右键,看url地址是不是你实际图片的存放地址。
string ipath = Server.MapPath("Images/"+name); //获取文件路径
修改为:
string ipath = "../Images/"+name ; //获取文件路径(2)
FileUpload1.SaveAs(ipath); //服务器保存路径
在把 修改为:
FileUpload1.SaveAs(Server.MapPath("./") +ipath); //服务器保存路径 Server.MapPath("./") +ipath 这路径是要自己调的!
你打个断点 看看那,是不是自己的想要的路径,不是,就调调
或Server.MapPath() +ipath
string ipath = Server.MapPath("Images/"+name); //获取文件路径上面这行去掉。
只保存文件名。
com.Parameters.Add("@Rent_Images", SqlDbType.VarChar, 50).Value = ipath;
改为
com.Parameters.Add("@Rent_Images", SqlDbType.VarChar, 50).Value = name;
显示再加上路径
<asp:Image ID="Image1" ImageUrl='<%# "Images/" + Eval("Rent_Images") %>' runat="server" />
(1)
string ipath = Server.MapPath("Images/"+name); //获取文件路径
修改为:
string ipath = "../Images/"+name ; //获取文件路径 (2)
FileUpload1.SaveAs(ipath); //服务器保存路径
在把 修改为:
FileUpload1.SaveAs(Server.MapPath("./") +ipath); //服务器保存路径 /**
Server.MapPath("./") +ipath 这路径是要自己调的!
你打个断点 看看那,是不是自己的想要的路径,不是,就调调
或Server.MapPath() +ipath
*/
看错了,string ipath = Server.MapPath("Images/"+name); 不用去掉
<appSettings>
<add key="UploadPath" value="Images/"/>
</appSettings>
if (FileUpload1.HasFile)
{
string uploadPath = System.Web.Configuration.WebConfigurationManager.AppSettings["UploadPath"];
string contentType = FileUpload1.PostedFile.ContentType;
if (contentType.Contains("image"))
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Table1(Rend_Names, Rend_Images) values(@Rend_Names, @Rend_Images)";
cmd.Parameters.AddWithValue("Rend_Names", TextBox1.Text);
cmd.Parameters.AddWithValue("Rend_Images", uploadPath + FileUpload1.FileName);
conn.Open();
cmd.ExecuteNonQuery();
}
FileUpload1.SaveAs(Server.MapPath(uploadPath) + FileUpload1.FileName);
}