【问题标题】:InputFile.PostedFile.ContentLength Units of measurementInputFile.PostedFile.ContentLength 测量单位
【发布时间】:2010-09-13 06:05:12
【问题描述】:

请告诉我 InputFile.PostedFile.ContentLength 测量的单位。我需要确保文件小于 500k。

谢谢。

【问题讨论】:

    标签: asp.net vb.net file-upload filesize maxstringcontentlength


    【解决方案1】:

    度量单位 = 字节。

    1 Kilobyte (kB) = 2ˆ10 Byte = 1024 Bytes
    

    文件大小为 15 KB 的示例代码测试:

    const int maxFileLength = 15360; // 15KB = 1024 * 15
    
    if(PictureFile.PostedFile.ContentLength > maxFileLength)
    {
    
        MyResult.Text = String.Format("Your post has a size of {0:#,##0} bytes which
        exceeded the limit of {0:#,##0} bytes. Please upload a smaller file.",
        PictureFile.ContentLength, maxFileLength);
    }
    else
    {
        // Save the file here
        MyResult.Text = "Thank you for posting."
    }
    

    在您的情况下,由于您希望文件小于 500 KB,因此您应该这样:

    const int maxFileLength = 512000; // 500KB = 500 * 1024
    

    【讨论】:

      【解决方案2】:

      字节数。

      http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.contentlength.aspx

      HttpPostedFile.ContentLength

      获取上传文件的大小,以字节为单位。

      【讨论】:

        【解决方案3】:

        当我上传超过 1 mb 的模板文件时,它会被加载很长时间。我的代码如下----

        if (checkImagecount())
        {
            //string imgFolder = Server.MapPath("/Userimages/" + emp_id.ToString().Trim() + "/Images/Browseimage/");
            string destinationImage = Server.MapPath("/master/" + emp_id.ToString().Trim() + "/Images/");
            string destination1 = Server.MapPath("/master/" + emp_id.ToString().Trim() + "/Images/");
            string imgFilename, fileExt = String.Empty;
            Response.Write(fileTemplateimage.PostedFile.ContentLength+"<br/>");
            Response.Write(fileTemplateimage.FileBytes.Length);
            Response.End();    
            if (fileTemplateimage.PostedFile.ContentLength > 0 && fileTemplateimage.FileBytes.Length <= 1048576)
            {
        
                System.Drawing.Image uploadImage = System.Drawing.Image.FromStream(fileTemplateimage.PostedFile.InputStream);
                float uploadImagewidth = uploadImage.PhysicalDimension.Width;
                float uploadImageheight = uploadImage.PhysicalDimension.Height;
        else
            {
        
                Page.ClientScript.RegisterStartupScript(this.GetType(), "failure", "alert('अपलोड फ़ाइल आकार 1 एमबी से अधिक नहीं होना चाहिए.');", true);
                divhide.Style.Add("display", "block");
                divUploadpanel.Style.Add("display", "block");
                fileTemplateimage.Focus();
        

        【讨论】:

          猜你喜欢
          • 2018-03-03
          • 2019-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-31
          • 2016-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多