请告诉我InputFile.PostedFile.ContentLength度量的单位。我需要确保文件小于500k。
谢谢。
最佳答案
度量单位=字节。
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