asp.net 文件 和图片上传

在aspx页面添加一个<INPUT id="fileUp" type="file" runat="server" name="file1">控件
                           一个Button控件

在aspx.cs页面的Button按钮事件中
string filePath="",fileExtName="",mFileName,mPath;
   System.Text.StringBuilder strMe 
= new System.Text.StringBuilder();
   
if(fileUp.PostedFile.FileName!="")
   
{
     filePath
=fileUp.PostedFile.FileName;
     fileExtName
=filePath.Substring(filePath.LastIndexOf(".")+1);
     
try
     
{
       mPath
=Server.MapPath("UpFile");
       
//取得文件名
        mFileName=filePath.Substring  (filePath.LastIndexOf("\\")+1);
       fileUp.PostedFile.SaveAs(mPath
+"/"+mFileName);   
       
//中间一定要加"/",不然会把UpFile文件夹名当成你上传文件   名的一部份
      }

     
catch(Exception eeee)
     
{

      }

}
 

如果图片上传到服务器并存到数据库中在不同的页面中取路径,则可只将   图片名存入数据库,图片放入某一目录。

然后另外的页面中取则可为

string patht=Request.ApplicationPath+"\\BusinessCard/"+Request.QueryString["strr"].Replace("'","");;
 Image1.ImageUrl
=patht;
Request.ApplicationPath为服务器根目录的物理路径
BusinessCard为名片目录, Request.QueryString["strr"].Replace("'","");为取得页面传过来的图片名并去掉传送过程中添加的'';
posted on 2005-11-18 13:50  泽来  阅读(645)  评论(0编辑  收藏  举报