各种格式化文件大小为字符串的函数FormatFileSize

前端之家收集整理的这篇文章主要介绍了各种格式化文件大小为字符串的函数FormatFileSize前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

Convert the Filesize in a Number into String Format
This code converts the filesize in a number to string format—for instance,1000 Bytes,1 KB,etc.
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
Sub Main()
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
'修改版本,毕竟现在硬盘刚刚出TB级别的,所以TB应该就够用的了。
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
End Module

----------------------

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";
}
}

或者调用API函数

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);

}

原文链接:https://www.f2er.com/vb/263191.html

猜你在找的VB相关文章