試試這個方法:
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
string savePath = @"c:\temp\uploads\";
if (FileUpload1.HasFile)
{
// Get the size in bytes of the file to upload.
int fileSize = FileUpload1.PostedFile.ContentLength;
// Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
if (fileSize < 2100000)
{
// Append the name of the uploaded file to the path.
savePath += Server.HtmlEncode(FileUpload1.FileName);
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it exceeds the 2 MB size limit.";
}
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
你看到這個答案? http://stackoverflow.com/a/1440804/882630 – lukkea 2012-04-04 11:07:47
可能的重複[查找文件大小與jQuery](http://stackoverflow.com/questions/1440723/find-file-size-with-jquery) – Gray 2012-04-04 12:37:44
@格雷 - 以前的評論可能重複! – lukkea 2012-04-04 12:58:21