【问题标题】:asp.net file uploadasp.net文件上传
【发布时间】:2011-04-04 17:38:17
【问题描述】:

我正在使用文件上传控件上传图像。 但它给出了错误, SaveAs方法配置为需要root路径,路径'~/Admin/ProductImages/images(5).jpg'没有root。

字符串 strBigServerPath = "~/Admin/ProductImages/"; 字符串 strFileName = ""; 字符串图像名 = "";

        if (prodImg.HasFile)
        {
            strFileName = prodImg.PostedFile.FileName;             

            prodImg.PostedFile.SaveAs(imgPath + strFileName);


            string[] ext = strFileName.Split('.');
            string newProductFileName = ext[0] + "123";
            ImageName = newProductFileName + "." + ext[1];
            prodImg.PostedFile.SaveAs(imgPath + ImageName);


            using (System.Drawing.Image Img =
              System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName))
            {
                if (Img.Width > 250 && Img.Height > 400)
                {
                    Size MainSize = new Size(250, 300);
                    using (System.Drawing.Image ImgThnail =
                           new Bitmap(Img, MainSize.Width, MainSize.Height))
                    {

                        ImgThnail.Save(Server.MapPath(strBigServerPath) + ImageName);

                    }
                }
                Img.Dispose();
            }

【问题讨论】:

    标签: asp.net file-upload


    【解决方案1】:

    您应该将绝对路径传递给SaveAs() 方法。 HostingEnvironment.ApplicationPhysicalPath Property 是获取 Web 应用程序根文件夹的便捷方式。

    var path = HostingEnvironment.ApplicationPhysicalPath + imgPath + strFileName;
    prodImg.PostedFile.SaveAs(path); 
    

    【讨论】:

      【解决方案2】:

      也许这会有所帮助:

      string filePath = System.IO.Path.Combine(Server.MapPath(imgPath), strFileName);
      prodImg.PostedFile.SaveAs(filePath);
      

      【讨论】:

        【解决方案3】:

        是不是因为你使用了

        prodImg.PostedFile.SaveAs(imgPath + strFileName);
        

        应该是什么时候

        prodImg.PostedFile.SaveAs(strBigServerPath + strFileName);
        

        【讨论】:

        • 正如其他人指出的那样,您可以使用 Server.MapPath() 方法将相对路径转换为绝对路径。
        【解决方案4】:

        使用时:

        prodImg.PostedFile.SaveAs(path);
        

        参数路径必须是一个绝对路径,你可以用这个来解析:

        Server.MapPath(strBigServerPath + filename)
        

        【讨论】:

          猜你喜欢
          • 2011-03-11
          • 2023-03-10
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 1970-01-01
          • 2015-04-18
          • 2018-01-21
          相关资源
          最近更新 更多