62,046
社区成员
发帖
与我相关
我的任务
分享
<tr>
<td align="right" style="text-align: right; width: 135px;">
图片上传:</td>
<td align="left" style="width: 228px">
<asp:FileUpload ID="FileUpload4" runat="server" />
<input id="Txt_Sell" runat="server" class="txt" maxlength="50" tabindex="4" type="text" visible="false" /></td>
</tr>
//上传文件处理
private void UPFile(string time)
{
string FilePath = Server.MapPath("./") + "File";
HttpFileCollection HFC = HttpContext.Current.Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile HF = HFC[i];
if (HFC[i].ContentLength > 0 && HFC[i].FileName != "")
{
//HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName));
string FName = HFC[i].FileName.Substring(HFC[i].FileName.LastIndexOf("."));
HF.SaveAs(FilePath + "//" + Convert.ToString(Convert.ToInt64(time) + i) + FName);//更改文件名
}
}
}
if (this.FileUpload1.HasFile)
{
string strPath = FileUpload1.FileName;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
FileUpload1.SaveAs.SaveAs(Server.MapPath("../UploadFile/")+fileName + extension);
strPath = fileName + extension;
using(SqlConnection conn =new SqlConnection(""))
{
conn .Open();
string sql = "";
SqlCommand cmd = new SqlCommand(sql, conn );
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch()
{
}
}
// 一般保存路径二进制使用
Stream s = FileUpload1.PostedFile.InputStream;
Byte[] buffer= new Byte[FileUpload1.PostedFile.ContentLength];
s.Read(buffer,0,FileUpload1.PostedFile.ContentLength);
string strsql = "insert into Tempimage(images,names)values(@ImageData,@names)";
SqlCommand cmd= new SqlCommand(strsql, conn);
cmd.Parameters.Add("@ImageData", SqlDbType.Image);
cmd.Parameters.Add("@names", SqlDbType.VarChar);
cmd.Parameters["@ImageData"].Value = buffer;
cmd.Parameters["@names"].Value = "";
cmd.ExecuteNonQuery();
//显示
Image1.ImageUrl="Photo.aspx?id="+Request.QueryString["id"];
photo.aspx
int Id=Request.QueryString["id"]==null?0:int.Parse(Request.QueryString["id"].ToString());
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from A where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
}
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";
sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ms.Close();
}
}
SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;
FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@image"].Value=box;
byte[] buffer = imageFile.FileData.get_BinaryData() as byte[];