【问题标题】:fileupload newline in constant文件上传换行符常量
【发布时间】:2011-03-25 05:29:05
【问题描述】:

我在常量中得到问题换行:

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        string theUserId = Session["UserID"].ToString();
        if (FileUploadControl.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);
                FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/) + filename);
//here
                StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }

}

我也想知道有没有办法在上传时调整图片的大小,如果可以的话,如何?

由于这是在我的文件上传控件中的保存方式,这是否会覆盖已存在用户 ID 的任何文件夹? (我想要和需要的)

【问题讨论】:

    标签: c# asp.net html


    【解决方案1】:

    常量换行

    FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/) + filename); //here

    应该是:

    FileUploadControl.SaveAs(Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/") + filename); //here

    图像调整大小/缩略图

    如果您想要的是缩略图,那么您可以使用Image.GetThumbnailImage。一个基本的实现是这样的:

    using (System.Drawing.Image fullsizeImage =
             System.Drawing.Image.FromFile(originalFilePath))
    {
           // these calls to RotateFlip aren't essential, but they prevent the image 
           // from using its built-in thumbnail, which is invariably poor quality.
           // I like to think of it as shaking the thumbnail out ;)
           fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
           fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    
           using (System.Drawing.Image thumbnailImage = 
             fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero))
           {
                thumbnailImage.Save(newFilePath);
           }
    }
    

    覆盖

    覆盖是默认行为,因此将相同的文件上传到相同的 UserID 将替换它。

    【讨论】:

    【解决方案2】:

    嗯,我已经可以在您的错误上方的行中看到您在 /uploadedimage/ 之后缺少一个引号,并且最后一个括号应该在文件名而不是 /uploadedimage/ 之后

    【讨论】:

      【解决方案3】:

      您是遗漏了引文还是错字:

      Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/")
      

      我还建议在使用大量字符串连接时使用string.Format(通常在我必须多次使用+ 时这样做):

      Server.MapPath(string.Format("~/userdata/{0}/uploadedimage/", theUserId))
      

      编辑:回答调整大小评论:

      这是一个类似 SO 问题的链接:

      how to resize an image whileing saving in an folder

      【讨论】:

        【解决方案4】:

        哪个值有换行变量?

        下面将 Trim() 添加到收集的值中,并且还提供了一种查看之后收集的每个字符串值的方法。

        protected void UploadButton_Click(object sender, EventArgs e)
        {
          string theUserId = Session["UserID"].ToString().Trim(); // add Trim()
          if (FileUploadControl.HasFile && !String.IsNullOrEmpty(theUserId)) // add test
          {
            try
            {
              string filename = Path.GetFileName(FileUploadControl.FileName);
              Console.WriteLine(filename);
              string filepath = string.Format("~/userdata/{0}/uploadedimage/", theUserId);
              Console.WriteLine(filepath );
              string mapPath = Server.MapPath(filepath);
              Console.WriteLine(mapPath );
              FileUploadControl.SaveAs(mapPath + filename);
              StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
              StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
          }
        }
        

        只需 Trim() 字符串值中有换行符常量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多