asp的:
Public Function formatdsize(dsize)
if dsize>=1073741824 then
formatdsize=Formatnumber(dsize/1073741824,2) & " GB"
elseif dsize>=1048576 then
formatdsize=Formatnumber(dsize/1048576,2) & " MB"
elseif dsize>=1024 then
formatdsize=Formatnumber(dsize/1024,2) & " KB"
else
formatdsize=dsize & "B"
end if
End Function
有用的思想:
ASP中file.filesize>2000000是限制到多少K呀?
<%dim sc_dx'//定义上传文件大小,单位KB
sc_dx=30
x.SetFileSize(sc_dx*1024) '最大文件大小
%>
这个sc_dx就是30KB了,这个方法可以用到以后的vb.net 上传图片到sql server里面。
VB.Net的:from: http://www.devx.com/tips/Tip/21185
Option Explicit ' Add a module and copy paste this function. ' To test this function,call this function from Immediate window by passing ' file size to it. e.g. ?GetFileSizeString(1000),GetFileSizeString(10000) Private Declare Function StrFormatByteSize Lib "shlwapi.dll" Alias "StrFormatByteSizeA" _ (ByVal dw As Long,ByVal szBuf As String,ByVal uiBufSize As Long) As Long Private Function GetFileSizeString(ByVal lngFileSize As Long) As String Dim strBuff As String strBuff = Space$(100) StrFormatByteSize lngFileSize,strBuff,Len(strBuff) GetFileSizeString = Replace(Trim$(strBuff),vbNullChar,"") End Function
------------------------Module Module1
Dim i As Long
'随便找一个数字来测试
i = 50850000000000000
Console.WriteLine("--{0}--",i)
Console.WriteLine("1. " & FormatFileSize(i,False))
Console.WriteLine("2. " & sFormatFileSize(i))
Console.WriteLine("3. " & nFormatFileSize(i))
Console.ReadLine()
End Sub
Function FormatFileSize(ByVal FileSize As Decimal,Optional ByVal DecimalDigits As Byte = 1,Optional ByVal Bytes As Boolean = True) As String
If FileSize = 0 Then Return "0" & IIf(Bytes,"bytes","bits") ' Prevent overflow
Dim Power As Integer = Math.Min(Math.Floor(Math.Log(Math.Ceiling(Math.Abs(FileSize)),1024)),8)
Dim Str As String = IIf(Power = 0,IIf(Bytes,"bits"),New String() {"KB","MB","GB","TB","PB","EB","ZB","YB"}(Power - 1))
Return Format(FileSize / (1024 ^ Power),"#." & StrDup(DecimalDigits,"0")) & " " & IIf(Bytes,Str,LCase(Str))
End Function
Function sFormatFileSize(ByVal FileSize As Decimal,Optional ByVal DecimalDigits As Byte = 1) As String
If FileSize = 0 Then Return "0"
Dim Power As Integer = Math.Min(Math.Floor(Math.Log(Math.Ceiling(Math.Abs(FileSize)),8)
Dim Str As String = New String() {"KB","YB"}(Power - 1)
Return Format(FileSize / (1024 ^ Power),"0")) & " " & Str
End Function
Function nFormatFileSize(ByVal FileSize As Decimal,4)
Dim Str As String = New String() {"KB","TB"}(Power - 1)
Return Format(FileSize / (1024 ^ Power),"0")) & " " & Str
End Function
----------------------
c#的:
Read more: "An example on how to format file size in C#" - http://www.waynejohn.com/post/2008/04/04/An-example-on-how-to-format-file-size-in-C.aspx#ixzz0ArSxzGDB
public class FileHelper
{
private static readonly long kilobyte = 1024;
private static readonly long megabyte = 1024 * kilobyte;
private static readonly long gigabyte = 1024 * megabyte;
private static readonly long terabyte = 1024 * gigabyte;
public static string ToByteString(long bytes)
{
if (bytes > terabyte)
return (bytes / terabyte).ToString("0.00 TB");
else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB");
else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0.00 KB");
else return bytes + " Bytes";
}
}
StrFormatByteSize (StrFormatByteSizeA,StrFormatByteSizeW or StrFormatByteSize64)
来源from:http://bytes.com/groups/net-c/515107-there-any-file-size-format-function-c
private string formatsizekb(double dsize)
{
const int iKB = 1024;
const long iMB = 1048576;
const long iGB= 1073741824;
if (dsize < iKB)
return string.format("{0} bytes",dsize);
if (dsize >= iKB && dsize < iMB)
return string.format("{0} KB",dsize/iKB);
if (dsize >= iMB && dsize < iGB)
return string.format("{0} MB",dsize/iMB);
if (dsize >= iGB )
return string.format("{0} GB",dsize/iGB);
}