目录

C上传图片到数据库问题

目录

C#上传图片到数据库问题

我用C#写了一段上传图片到数据库的代码,思路是:把图片上传到指定文件夹,然后把路径发送到数据库中,显示这个图片时,直接从数据库中读取路径来显示图片,网上找了很久,都不行.我用C#写的代码,很简单的几句,上传的功能已进ok了,但是把路径写入数据库这个环节还有点问题.

我的思路: (upload.aspx)  上传图片–>获取路径–> 向数据库写入路径

pic.asp(显示图片页)    读取图片路径–>src="###"

目前C#不是很熟悉,所以用了asp和C#合用的手法,上传用C#,显示用asp

-----upload.aspx-----

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="_upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
">
  
<html xmlns="
<http://www.w3.org/1999/xhtml>
">
  
<head runat="server">
  
<title>无标题页</title>
  
</head>
  
<body>
  
<form id="form1" runat="server" enctype="multipart/form-data" >
  
<div>
  
<input id="File1" type="file" runat="server" />
  
<input id="Button1" type="button" value="上传" onserverclick="Button1_ServerClick" runat="server" />
  
</div>
  
</form>
  
</body>
  
</html>
-----upload.aspx.cs-----

using System;
  
using System.Data;
  
using System.Configuration;
  
using System.Collections;
  
using System.Web;
  
using System.Web.Security;
  
using System.Web.UI;
  
using System.Web.UI.WebControls;
  
using System.Web.UI.WebControls.WebParts;
  
using System.Web.UI.HtmlControls;

public partial class _upload : System.Web.UI.Page
  
{
  
protected void Page_Load(object sender, EventArgs e)
  
{

}
  
protected void Button1_ServerClick(object sender, EventArgs e)
  
{
  
ArrayList arrfilename = new ArrayList();
  
arrfilename.AddRange(File1.Value.Split('//'));
  
string filename = arrfilename[arrfilename.Count - 1].ToString();

string uploadpath = Page.Request.PhysicalApplicationPath + "pic//";

File1.PostedFile.SaveAs(uploadpath + filename);
  
Page.Response.Write("文件上传成功!");
  
Page.Response.Write(uploadpath);

}
  
}