求教:如何上传图片(只上传图片路径)并实现图片显示 asp.net(c#) 
能给个例子么?

解决方案 »

  1.   

    var path='<%=myPath%>';
    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">
      

  2.   

    <%@ Page language="c#" Src="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %> 
    <!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 

    }
      

  3.   

    谢谢各位luojianfeng的信息很详细,luck0235思路很明确.钱少,少给点,礼轻情谊重