开发者

Display a pdf file size value in a gridview in asp.net

My sql query is returning records from the database that contains a filename. I will be appending the filepath (from the web.config file) to allow the user 开发者_StackOverflow中文版to download the complete file. However, another colum in the grid, has to be the file size.

How can I obtain the filesize, given that the name is being returned from the database, and the filepath is from the web.config file?

Thanks


One way to do it is to store the size of the pdf file upon your upload of the file to the server and then just retrieve it along with the file name.


Using System.IO you can get the filesize

Dim f As New FileInfo("thefullfilepath")
Dim i As Integer = f.Length

EDIT: Use Import System.IO for VB


string MyFile = "~/files/my.pdf";

FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;


Use the FileInfo class:

' Build the string fileName from path stored in Web.Config and the filename
' returned from the database
Dim fileName As String = "your path and filename"
Dim fInfo As FileInfo = new FileInfo(fileName)
Dim fileSize As Long = fInfo.Length

You will probably need to do this in the RowDataBound event, assuming your binding your GridView to a datasource (like your SQL query).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜