c# 上传图片
<asp:TextBox ID="TextBox1" runat="server" Width="98%" CssClass="wuxian"/>
<asp:TextBox ID="TextBox2" runat="server" Width="98%" CssClass="wuxian"/>
<img id="img1" style="width:80px;height:80px" runat="server"/> <input type="file" id="file" />
<input id="img1str64" type="hidden" runat="server"/>
<asp:Button runat="server" ID="btn_bc_3" Style="width: 100px;background-color:#7abce7;color:white;" Text="保存" OnClientClick="buttonClick();" OnClick="btn_bc_3_Click" />
function buttonClick(){ $('#<%=TextBox1.ClientID%>').val(aimg); $('#<%=TextBox2.ClientID%>').val(aimg64); }
var input = document.getElementById("file"); //input file input.onchange = function () { var file = this.files[0]; if (!!file) { //读取本地文件,以gbk编码方式输出 var reader = new FileReader(); reader.onload = function (e) { aimg=e.target.result; aimg64=e.target.result.toString().split(',')[1]; $("#img1").attr("src", e.target.result); $("#img1str64").val(e.target.result.toString().split(',')[1]); } reader.readAsDataURL(file); } }
后台
string a = this.TextBox2.Text;
GetPictureData(a, "");
public string GetPictureData(string imagepath, string imgname) { //根据图片文件的路径使用文件流打开,并保存为byte[] DateTime dt = DateTime.Now; string paths = Server.MapPath("/Image/").ToString(); if (System.IO.Directory.Exists(paths + dt.Year)) //己经存在 年文件夹 { if (System.IO.Directory.Exists(paths + dt.Year + "/" + dt.Month)) //己经存在 月文件夹 { } else { System.IO.Directory.CreateDirectory(paths + dt.Year + "/" + dt.Month); } } else { System.IO.Directory.CreateDirectory(paths + dt.Year); if (System.IO.Directory.Exists(paths + dt.Year + "/" + dt.Month)) //己经存在 月文件夹 { } else { System.IO.Directory.CreateDirectory(paths + dt.Year + "/" + dt.Month); } } // FileStream fs = new FileStream(imagepath, FileMode.Open); byte[] accessory = Convert.FromBase64String(imagepath); //byte[] byData = new byte[fs.Length]; System.IO.Stream s = new System.IO.MemoryStream(accessory); System.Drawing.Image image = System.Drawing.Image.FromStream(s); string pathstr = Server.MapPath("/Image/" + dt.Year + "/" + dt.Month + "/").ToString() + dt.ToString("yyyyMMddhhmmssfff") + imgname + ".jpg"; // string pathstr = Server.MapPath("/Image/"+ dt.Year + "/" + dt.Month + "/") + dt.ToString("yyyyMMddhhmmssfff") + imgname + ".jpg"; // string pathstr = "d:/Image/" + dt.Year + "/" + dt.Month + "/" + dt.ToString("yyyyMMddhhmmssfff") + imgname + ".jpg"; string sqlpath = "/Image/" + dt.Year + "/" + dt.Month + "/" + dt.ToString("yyyyMMddhhmmssfff") + imgname + ".jpg"; image.Save(pathstr); return sqlpath; }
再三须慎意,第一莫欺心