求教:如何上传图片(只上传图片路径)并实现图片显示 asp.net(c#)
能给个例子么?
能给个例子么?
解决方案 »
- . Net 日期控件
- 有个不理解得问题????请指点一下
- |M| 第二贴:当遇上这样的对数据库记录进行更新的时候大家是怎么做的(上一贴写得不清楚)
- 我想在Asp.net页面中操作客户端的DLL,就像ActiveX控件一样,是否有好的方案
- ASP.net上传文件如何做个关联的进度条
- datagrid的updata事件,怎么显示只能编辑一个列??我的3个列都可以编辑,我只要1个编辑就够了
- 为什么日志中记录的时间跟电脑时间不同
- 如何将服务器端文件下载到客户端临时文件夹or指定文件夹(要写死)
- asp.net中插入中文无法识别问题???大虾帮忙!!!!thank you!!!
- 为什么我建的Web应用程序执行后无法显示页面上的控件?一定给分
- 如何實現ASP.NET發送内部Notes郵件的代碼?
- DataSet修改和删除记录后,再一次性更新到数据库,删除的记录没有更新。
function setImg(path)
{
document.all.preview.innerHTML="<img src='"+path+"' width='200'>";
}<INPUT id="myFile" type="file" onchange="setImg(this.value);" size="60" name="myFile" runat="server">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>文件上傳</title>
</HEAD>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上傳</h3>
<P id="MyFile"><INPUT type="file" size="50" NAME="File"></P><font color=red>* 上傳文件不能超過10M</font>
<P>
<asp:Button Runat="server" Text="開始上傳" ID="UploadButton"></asp:Button>
</P>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</P>
</div>
</form>
</body>
</HTML>
-----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;namespace WebPortal
{
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus; private void Page_Load(object sender, System.EventArgs e)
{
Session["uptime"]=DateTime.Now.Date.ToShortDateString();
Session["uptime2"]=DateTime.Now.ToString().Replace("下午","").Replace("上午","").Replace("/","").Replace("-","").Replace(":","").Replace(" ","");
if (this.IsPostBack) this.SaveImages();
} private Boolean SaveImages()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上傳的文件分別是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'?查文件?展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension,fileSize;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if((postedFile.ContentLength)>10485760)
{
Response.Write("<script>alert('上傳文件的文件不能超過10M!');</script>");
}
else
{
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上傳的文件類型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客戶端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上傳文件的文件名:" + fileName + "<br>");
strMsg.Append("上傳文件的擴展名:" + fileExtension + "<br>");
strMsg.Append("上傳文件的路?:" + Request.PhysicalApplicationPath + "uploadfile"+"<br>");
strMsg.Append("上?文件的路?:" + Server.MapPath(fileName) + "<br>");
strMsg.Append("上傳文件的路徑:" + "uploadfile/"+ fileName +"<br>");
strMsg.Append("上傳文件的大小:" + countSize(postedFile.ContentLength) + "<br><hr>");
///'可根据?展名字的不同保存到不同的文件?
///注意:可能要修改你的文件?的匿名?入?限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../uploadfile/") + fileName);
//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../uploadfile/") + Session["uptime2"]+".xls"); string nowDSN=ConfigurationSettings.AppSettings["data"];
SqlConnection myConnection=new SqlConnection(nowDSN);
string CommandText;
SqlCommand myCommand = new SqlCommand();
myCommand.Connection=myConnection;
myCommand.Connection.Open();
string filename3=Session["uptime2"]+".xls";
myCommand.CommandText = "insert into uploadfile(filename,filesize,filemappath,fileExtension,uptime,filename2)values('"+fileName+"','"+countSize(postedFile.ContentLength)+"','"+Request.PhysicalApplicationPath+"uploadfile\\"+fileName+"','"+fileExtension+"','"+Session["uptime"]+"','"+filename3+"')";
myCommand.ExecuteNonQuery();}
else
{
Response.Write("<script>alert('上傳文件的文件不能為空!');</script>");
}
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体??器生成的代?
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);
} public string countSize(long size) //?是?算文件大小的方法
{
string strSize="";
long lnSize=0;
lnSize=size;
if(lnSize<1024)
strSize=lnSize.ToString()+"byte";
if(lnSize>=1024 && lnSize<1048576)
strSize=(lnSize/1024).ToString() + "K";
if(lnSize>=1048576 && lnSize<=10485760)
strSize=(lnSize/1024/1024).ToString() + "M";
return strSize;
} #endregion
}
}