文件上传路径

Server.MapPath:返回与WEB服务器上虚拟路径相对应的物理文件路径。
Request.ApplicationPath:获取服务器上ASP.NET虚拟应用程序的根路径。
现在根路径下有images(储存上传的图片),files(储存上传的文件),新建文件夹1(此文件夹下有上传的ASP页面fileload.aspx)。如果现在用下面的1.语句得到物理文件路径,获得的是“新建文件夹1\images\"+fileName,而新建文件夹1下并无images文件夹,这样将得到错误的上传和保存路径。因此必须修改为1.语句下面的语句。


object sender, EventArgs e)
    {
        string fileName = FileUpload1.FileName;
        
        string fileSize = FileUpload1.PostedFile.ContentLength.ToString();
        string fileType = FileUpload1.PostedFile.ContentType;
        string fileType1 = fileName.Substring(fileName.LastIndexOf(".") + 1);
       1. //string upImagePath = Server.MapPath("images") + "\\" + fileName; 
        string upImagePath = Server.MapPath(Request.ApplicationPath + "/images") + "\\" + fileName;
       2./string upFilePath = Server.MapPath("files") + "\\" + fileName;
        string upFilePath = Server.MapPath(Request.ApplicationPath + "/files") + "\\" + fileName;
       3./string databasePath = "images\\" + fileName;
        string databasePath = Request.ApplicationPath + "/images" + "/" + fileName;
        if (fileType1 == "jpg" || fileType1 == "bmp" || fileType1 == "gif")
        {
            FileUpload1.SaveAs(upImagePath);
            Image1.Visible = true;
            Image1.ImageUrl = databasePath;
            Label1.Text = "上传的文件名是:" + fileName + "<br>" + "文件的大小是:" + fileSize + "<br>" + "文件的类型为:" + fileType + "<br>" + "文件的扩展名是:" + fileType1 + "<br>" + "保存的实际路径是:" + upImagePath;
        }
        else
        {
            FileUpload1.SaveAs(upFilePath);
            Image1.Visible = false;
            Label1.Text = "上传的文件名是:" + fileName + "<br>" + "文件的大小是:" + fileSize + "<br>" + "文件的类型为:" + fileType + "<br>" + "文件的扩展名是:" + fileType1 + "<br>" + "保存的实际路径是:" + upFilePath;
        }
    }
posted @ 2007-12-28 10:26  guoan  阅读(1542)  评论(0)    收藏  举报