求一段上传图片的代码 如题,请问如何上传图片,最好能有一段简单详细的代码,在线等,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <table> <tr> <td colspan="2" style="height: 21px" > 使用标准HTML来进行图片上传</td> </tr> <tr> <td style="width: 400px"> <input id="InputFile" style="width: 399px" type="file" runat="server" /></td> <td style="width: 80px"> <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /></td> </tr> <tr> <td colspan="2" > <asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td> </tr> </table> string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名 //string uploadName = InputFile.PostedFile.FileName; string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 if (InputFile.Value != "") { int idx = uploadName.LastIndexOf("."); string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 pictureName = DateTime.Now.Ticks.ToString() + suffix; } try { if (uploadName != "") { string path = Server.MapPath("~/images/"); InputFile.PostedFile.SaveAs(path + pictureName); } } catch (Exception ex) { Response.Write(ex); } http://www.cnblogs.com/qiantuwuliang/archive/2009/08/21/1551200.html this.File1.PostedFile.SaveAs(Server.MapPath(目录)+文件名); <div id="myimg"> <%-- <asp:Image ID="newPreview" runat="server" />--%> <img id="newPreview" alt="我的头像" src="" runat="server" /> </div> <div id="MyFileUpload1"> <asp:FileUpload ID="FileUpload1" Width="250px" runat="server" onchange="showprepic(this.value)" Height="22px" /></div>新建立一个JS文件 放进去的代码 这个是让图片及时预览的JS //上传图片时预览 function PreviewImg(FileUpload1){ //显示图片 var newPreview = document.getElementById("newPreview"); newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = FileUpload1.value; newPreview.style.display = "block"; } function showprepic(file){ var newPreview = document.getElementById("newPreview"); newPreview.src = ""; newPreview.src = "file://" + file; newPreview.style.display = "block"; } var flag=false; function DrawImage(ImgD){ var image=new Image(); image.src=ImgD.src; if(image.width>0 && image.height>0){ flag=true; if(image.width/image.height>= 120/80){ if(image.width>120){ ImgD.width=120; ImgD.height=(image.height*320)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; } else{ if(image.height>80){ ImgD.height=80; ImgD.width=(image.width*80)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; } } /**//**//**//*else{ ImgD.src=""; ImgD.alt="" }*/ } 后台 代码 #region 上传图片 public string ShowPotole() { try { string fileName = ""; if (this.FileUpload1.HasFile)//检查是否有文件 { string fullFileName = this.FileUpload1.PostedFile.FileName;//文件路径名 fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称 string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式 if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型 { if (this.FileUpload1.PostedFile.ContentLength > 200 * 1024) { Response.Write("<script>alert('上传图片必须小于50k!');</script>"); return ""; } else { string path = HttpContext.Current.Request.MapPath("~/img/");//获取上传文件的网站目录路径 if (File.Exists(Server.MapPath("~/img/" + fileName)))//(这个地方做判断是不是 有这 张图片如果已经有图片 则不能上传 ) { return ""; } else { this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘 // this.newPreview.InnerText. = "~/admin/img/" + fileName;//显示图片 // this.newPreview.InnerHtml = "~/admin/img/" + fileName;//显示图片 } } } else { Response.Write("<script>alert('非图片类型,不允许上传!');</script>"); return ""; } } else { if (HiddenField1.Value != null && HiddenField1.Value != "") { fileName = ArticleManager.Instance.GetArticleByArticleid(int.Parse(HiddenField1.Value)).Giftimg; } else Response.Write("<script>alert('文件未指定,你确定要添加!');</script>"); } return fileName; } catch (Exception) { throw; } } #endregion 谢谢,请问 //上传图片时预览 function PreviewImg(FileUpload1)如何触发 http://www.cnblogs.com/cloudgamer/archive/2009/12/22/ImagePreview.html 用VS2008做个压力测试,25个用户,其他都是默认,怎么错误那么多? js 内如何判断 指定 div1 内是否包含 name 属性为 "abc" 的控件呢? 在线求助 如何在客户端执行DLL? 请问C#里,符合时间格式的字符串如何转为时间格式! 请问怎么设置文本控件的格式 水晶报表10的web打印为何没有反应? 如何判断数据库? 请帮忙,SQL语句中的where语句错误 菜鸟问题:对DataGrid事件UpdateCommand的如何用参数来更新数据!!!!!! 上传文件的问题 如何截取这样的字符串?
<tr>
<td colspan="2" style="height: 21px" >
使用标准HTML来进行图片上传</td>
</tr>
<tr>
<td style="width: 400px">
<input id="InputFile" style="width: 399px" type="file" runat="server" /></td>
<td style="width: 80px">
<asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /></td>
</tr>
<tr>
<td colspan="2" >
<asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
</table>
string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value != "")
{
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString() + suffix;
}
try
{
if (uploadName != "")
{
string path = Server.MapPath("~/images/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
}
catch (Exception ex)
{
Response.Write(ex);
} http://www.cnblogs.com/qiantuwuliang/archive/2009/08/21/1551200.html
<%-- <asp:Image ID="newPreview" runat="server" />--%>
<img id="newPreview" alt="我的头像" src="" runat="server" />
</div>
<div id="MyFileUpload1"> <asp:FileUpload ID="FileUpload1" Width="250px" runat="server" onchange="showprepic(this.value)"
Height="22px" /></div>新建立一个JS文件 放进去的代码 这个是让图片及时预览的JS
//上传图片时预览
function PreviewImg(FileUpload1){
//显示图片
var newPreview = document.getElementById("newPreview");
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = FileUpload1.value;
newPreview.style.display = "block";
}
function showprepic(file){ var newPreview = document.getElementById("newPreview");
newPreview.src = "";
newPreview.src = "file://" + file;
newPreview.style.display = "block";
}
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 120/80){
if(image.width>120){
ImgD.width=120;
ImgD.height=(image.height*320)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>80){
ImgD.height=80;
ImgD.width=(image.width*80)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
/**//**//**//*else{
ImgD.src="";
ImgD.alt=""
}*/
}
后台 代码
#region 上传图片 public string ShowPotole()
{
try
{
string fileName = "";
if (this.FileUpload1.HasFile)//检查是否有文件
{
string fullFileName = this.FileUpload1.PostedFile.FileName;//文件路径名
fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式 if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
{
if (this.FileUpload1.PostedFile.ContentLength > 200 * 1024)
{
Response.Write("<script>alert('上传图片必须小于50k!');</script>");
return "";
}
else
{
string path = HttpContext.Current.Request.MapPath("~/img/");//获取上传文件的网站目录路径 if (File.Exists(Server.MapPath("~/img/" + fileName)))//(这个地方做判断是不是 有这 张图片如果已经有图片 则不能上传 )
{
return "";
}
else
{
this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘
// this.newPreview.InnerText. = "~/admin/img/" + fileName;//显示图片
// this.newPreview.InnerHtml = "~/admin/img/" + fileName;//显示图片
}
}
}
else
{
Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
return "";
}
}
else
{
if (HiddenField1.Value != null && HiddenField1.Value != "")
{
fileName = ArticleManager.Instance.GetArticleByArticleid(int.Parse(HiddenField1.Value)).Giftimg;
}
else
Response.Write("<script>alert('文件未指定,你确定要添加!');</script>");
}
return fileName;
}
catch (Exception)
{ throw;
}
}
#endregion
谢谢,请问 //上传图片时预览
function PreviewImg(FileUpload1)如何触发