上传图片到数据库,然后在页面中显示出来,新手求教啊,好心人帮帮忙
解决方案 »
- B/S系统,sqlite数据库存在客户U盘里面,访问网站的时候,网站可以读取U盘里面的数据库吗?
- mvc 的 create 和update 方法的 view 怎样 使用同一个view?
- DataSet操作XML的问题
- 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
- .net 2005建立网站的问题!
- 体现一下你的实力,很难解决的一个问题?
- 续方才提出的asp.net加入javascript时钟问题?多谢大家!
- 例如一个网上书店:怎样统计出近一个月点击率最高的书?用什么原理的算法????
- 大家给点思路,定时发送邮件或者短信
- 在asp.net web 应用程序中,怎样使用“密码输入框”,我用textbox不可以把字符串变为“**”啊,怎么办
- 关于对第三方网站进行点击统计,留言的设计
- gridview不能及时刷新
{
if (fu.HasFile)
{
string fileContentType = fu.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = fu.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
//取得当前日期时间
string strDate = DirFile.GetDateDir();
string strTime = DirFile.GetDateFile();
//创建目录
DirFile.CreateDir(fileDir + strDate);
string fileName = strDate + strTime + file.Name.Substring(file.Name.LastIndexOf(".")); // 文件名称file.Name
//string fileName_s = strDate + strTime + "s" + file.Name.Substring(file.Name.LastIndexOf(".")); // 文件名称file.Name
string webFilePath = Server.MapPath("~" + fileDir + strDate + "/" + fileName); // 服务器端文件路径
//string webFilePath_s = Server.MapPath("/UploadFiles/upload/news/" + strDate + "/" + fileName_s);// 服务器端缩略图文件路径
if (!File.Exists(webFilePath))
{
try
{
fu.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
//pic.MakeThumbnail(webFilePath, webFilePath_s, 250, 220, "HW"); // 生成缩略图方法
tb.Text = strDate + "/" + fileName;
PublicFc.Show(this, "恭喜你,文件上传成功!");
//DirFile.DeleteFile("/UploadFiles/upload/news/" + strDate + "/" + fileName);
img.ImageUrl = "../.." + fileDir + this.txtImg.Text;
}
catch (Exception ex)
{
PublicFc.Show(this, "提示:文件上传失败,失败原因:" + ex.Message);
}
}
else
{
PublicFc.Show(this, "提示:文件已经存在,请重命名后上传!");
}
}
else
{
PublicFc.Show(this, "提示:文件类型不符,只支持.gif、.bmp和.jpg图片类型!");
}
}
}
<td style="height: 25px; width: 100px; text-align: right;">
新闻图片:</td>
<td>
<asp:Image ID="imglink_pic" runat="server" Width="150px" Height="120px" />
<br />
<asp:TextBox ID="txtImg" runat="server" Width="250px" ReadOnly="True"></asp:TextBox>
<br />
<asp:FileUpload ID="fuImg" runat="server" Width="260px" />
<asp:Button ID="btnImgUpload" runat="server" Text="上传" OnClick="btnImgUpload_Click" />
*上传前请注意比例,防止图片过大或变形*
</td>
</tr>
本地图片访问的路径是:http://localhost/images/20120107/test.jpg数据库存的路径是:http://localhost/images/20120107/test.jpg 这是存的绝对的路径,在前台直接绑定出来就行了。数据库存的路径是:20120107/test.jpg 这是存的相对的路径,要用相应路径来绑定。
前台绑定代码:<image src="/<%=imagpath%>">如果有不明白的,请加QQ:79522860
http://www.2cto.com/kf/201109/103586.html
http://blog.163.com/sdq_1314/blog/static/166908254201081704330711/如果是存的路径请参考hailang2ll的思路
id="dlstImages"
DataSourceID="srcImages"
RepeatColumns="3"
Runat="server">
<ItemTemplate>
<asp:Image ID="Image1"
ImageUrl='<%# String.Format("DynamicImage.ashx?id={0}", Eval("Id")) %>'
Width="250"
Runat="server" />
<br />
<%# Eval("Description") %>
</ItemTemplate>
</asp:DataList>
<hr /> <asp:FormView
id="frmImage"
DataSourceID="srcImages"
DefaultMode="Insert"
Runat="server">
<InsertItemTemplate>
<asp:Label
id="lblImage"
Text="Upload Image:"
AssociatedControlId="upImage"
Runat="server" />
<br />
<asp:FileUpload
id="upImage"
FileBytes='<%# Bind("Image") %>'
Runat="server" />
<br /><br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<br />
<asp:TextBox
id="txtDescription"
Text='<%# Bind("Description") %>'
TextMode="MultiLine"
Columns="50"
Rows="2"
Runat="server" />
<br /><br />
<asp:Button
id="btnInsert"
Text="Add Image"
CommandName="Insert"
Runat="server" />
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource
id="srcImages"
SelectCommand="SELECT ID,Description FROM Images"
InsertCommand="INSERT Images (Image,Description)
VALUES (@Image,@Description)"
ConnectionString="<%$ ConnectionStrings:Images %>"
Runat="server" />
------------------------------------------------------------------
ashx
<%@ WebHandler Language="C#" Class="DynamicImage" %>using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;/// <summary>
/// Displays an image corresponding to the Id passed
/// in a query string field
/// </summary>
public class DynamicImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
// Get the Id of the image to display
string imageId = context.Request.QueryString["Id"];
// Use SqlDataSource to grab image bytes
SqlDataSource src = new SqlDataSource();
src.ConnectionString = WebConfigurationManager.ConnectionStrings["Images"].ConnectionString;
src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId; // Return a DataView
DataView view = (DataView)src.Select(DataSourceSelectArguments.Empty);
context.Response.BinaryWrite( (byte[])view[0]["Image"]); // Return a DataReader
//src.DataSourceMode = SqlDataSourceMode.DataReader;
//IDataReader reader = (IDataReader)src.Select(DataSourceSelectArguments.Empty);
//reader.Read();
//context.Response.BinaryWrite((byte[])reader["Image"]);
//reader.Close();
}
public bool IsReusable
{
get
{
return false;
}
}}
显示时在请求转化,