当前位置:编程学习 > C#/ASP.NET >>

求助:文件上传问题!未能找到文件“C:\Documents and Settings\Administrator\桌面\221.jpg”。

在本机测试没有问题
放在服务器的然后就报错

未能找到文件“C:\Documents and Settings\Administrator\桌面\221.jpg”。


String fileTypes = "gif,jpg,jpeg,png,bmp";
            //最大文件大小
            int imgMaxSize = Convert.ToInt32(MaxSize) * 1024 * 1024;

            HttpPostedFile imgFile = FuImg.PostedFile;
            if (imgFile == null || FuImg.FileName == "")
            {
                Alert("请选择文件。");
                return "";
            }
            String dirPath = SavePath;
            string saveUrl = SaveUrl;
            if (!Directory.Exists(dirPath))
            {
                Alert("上传目录不存在。");
                return "";
            }

            String fileName = imgFile.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();
            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));

            if (imgFile.InputStream == null || imgFile.InputStream.Length > imgMaxSize)
            {
                Alert("上传文件大小超过限制。");
                return "";
            }

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                Alert("上传文件扩展名是不允许的扩展名。");
                return "";
            }
            //验证文件格式
            if (!IsAllowedExtension(imgFile, fileTypes))
            {
                Alert("图片格式不正确。");
                return "";
            }


            String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
            dirPath += ymd + "/";
            saveUrl = SaveUrl + ymd + "/";
            //判断目录是否存在
            if (!Directory.Exists(dirPath))
            {
                //创建目录
                Directory.CreateDirectory(dirPath);
            }

            String newFileName = Guid.NewGuid().ToString() + fileExt;//图片名字
            String filePath = dirPath + newFileName;

            imgFile.SaveAs(filePath);

            String fileUrl = SaveUrl + newFileName;
            ImageUrl = Img.ImageUrl = saveUrl + newFileName;
            //ImageUrl = saveUrl + newFileName;
            txtedit.Text = "";
--------------------编程问答-------------------- 在服务器上报“未能找到文件“C:\Documents and Settings\Administrator\桌面\221.jpg”。”,你服务器这个地址有这个图片吗? --------------------编程问答-------------------- --------------------编程问答-------------------- 哈哈,抄来抄去,把对的抄成错的了!太牛了!

--------------------编程问答-------------------- 把图片放其他盘里试试,你那个saveurl是服务器保存路径吗? --------------------编程问答--------------------
引用 2 楼  的回复:
IsAllowedExtension这个函数错误的
网上都是抄袭,不知道这是哪个菜鸟写的还都抄来抄去的

这个函数采用客户端的路径处理,是不正确的

我发现了确实是这里的问题。但是有没有方法可以做服务器端的呢?我想了解下。 --------------------编程问答--------------------
引用 5 楼  的回复:
引用 2 楼  的回复:
IsAllowedExtension这个函数错误的
网上都是抄袭,不知道这是哪个菜鸟写的还都抄来抄去的

这个函数采用客户端的路径处理,是不正确的

我发现了确实是这里的问题。但是有没有方法可以做服务器端的呢?我想了解下。


可以做的。我写过 --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,