通过使用Server.Mappath()和FileUpload.SaveAs(文件上传) [英] File Uploading using Server.MapPath() and FileUpload.SaveAs()

查看:244
本文介绍了通过使用Server.Mappath()和FileUpload.SaveAs(文件上传)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站管理员节这我忙工作,具有特定用途4的FileUpload控件。我需要知道的是,当我使用使用Server.Mappath()方法中FileUpload控件的另存为()方法,将它仍然是在Web服务器上可用的我已经上传网站后?据我所知,另存为()需要一个绝对路径,这就是为什么我用地图的路径使用Server.Mappath()

 如果(fuLogo.HasFile)//我FileUpload控件:检查文件是否已经被分配给控制
        {
            INT计数器= 0; //这个计数器用于确保没有文件将被覆盖。
            字符串[] = fileBreak fuLogo.FileName.Split(新的char [] {''});
            LOGO =使用Server.Mappath(../图像/徽标/+ fileBreak [0] + counter.ToString()+ + fileBreak [1]。); //这是我想知道有关的部分。这是否会仍然起作用,应该在Web服务器上上传后的方法是什么?
            如果(fileBreak [1] .ToUpper()==GIF|| fileBreak [1] .ToUpper()==PNG)
            {
                而(System.IO.File.Exists(标识))
                {
                    反++; //这里计数器付诸行动
                    LOGO =使用Server.Mappath(../图像/徽标/+ fileBreak [0] + counter.ToString()+ + fileBreak [1]。);
                }
            }
            其他
            {
                cvValidation.ErrorMessage =该网站不支持超过png格式或.gif任何其他图像格式,请保存这些文件格式之一的图片,然后再试一次。
                cvValidation.IsValid = FALSE;
            }
            如果(fuLogo.PostedFile.ContentLength> 409600)//文件必须是400KB或更小
            {
                cvValidation.ErrorMessage =请使用图片的尺寸小于400 KB
                cvValidation.IsValid = FALSE;            }
            其他
            {                如果(fuLogo.HasFile&安培;&安培; cvValidation.IsValid)
                {
                    fuLogo.SaveAs(标识); //保存标识文件是否存在,并验证没有失败。徽标的路径是由使用Server.Mappath()方法创建。
                }            }
        }
        其他
        {
            标志=N / A;
        }


解决方案

  • 如果您要保存在文件
    您的Web服务器上的目录,然后
    使用Server.Mappath()将是合适的
    解决方案。

    字符串dirPath = System.Web.HttpContext.Current.Server.MapPath(〜)+/图像/徽标/+ fileBreak [0] + counter.ToString()+。 + fileBreak [1];

    这里


  • 如果您要保存文件出来
    然后Web服务器

    使用一个完整路径,如C:\\上传
    并确保网络进程
    写权限到该文件夹​​,我建议你保存路径本身,在这种情况下,web.config文件。


I have a website admin section which I'm busy working on, which has 4 FileUpload controls for specific purposes. I need to know that , when I use the Server.MapPath() Method Within the FileUpload control's SaveAs() methods, Will it still be usable on the web server after I have uploaded the website? As far as I know, SaveAs() requires an absolute path, that's why I map a path with Server.MapPath()

if (fuLogo.HasFile) //My FileUpload Control : Checking if a file has been allocated to the control
        {
            int counter = 0;  //This counter Is used to ensure that no files are overwritten.
            string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
            logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]);  // This is the part Im wondering about. Will this still function the way it should on the webserver after upload?
            if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
            {
                while (System.IO.File.Exists(logo))
                {
                    counter++; //Here the counter is put into action
                    logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
                }
            }
            else
            {
                cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
                cvValidation.IsValid = false;
            }
            if (fuLogo.PostedFile.ContentLength > 409600 )  //File must be 400kb or smaller
            {
                cvValidation.ErrorMessage = "Please use a picture with a size less than 400 kb";
                cvValidation.IsValid = false;

            }
            else
            {

                if (fuLogo.HasFile && cvValidation.IsValid)
                {
                    fuLogo.SaveAs(logo); //Save the logo if file exists and Validation didn't fail. The path for the logo was created by the Server.MapPath() method.
                }

            }
        }
        else
        {
            logo = "N/A";
        }

解决方案

  • If you intend to save the files in a directory on your web server , then the Server.MapPath() will be the suitable solution.

    string dirPath = System.Web.HttpContext.Current.Server.MapPath("~") + "/Images/Logos/"+ fileBreak[0] + counter.ToString() + "." + fileBreak[1];

    Look Here

  • if you intend to save your files out the web server then

    Use a full path, like "c:\uploads" and be sure that the web process has permission to write to that folder,I suggest you store the path itself in the web.config file in this case.

这篇关于通过使用Server.Mappath()和FileUpload.SaveAs(文件上传)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆