1. /*
  2. ASP.NET 默认上传文件是4M ,可以修改服务配置..
  3.  
  4. <system.web>
  5. <!-- 指示 ASP.NET 支持的最大文件上载大小。
  6. 该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。
  7. 指定的大小以 KB 为单位。默认值为 4096 KB (4 MB)。最大为:2097151 KB
  8. 此处改为40M大小的文件上传限制。
  9. -->
  10. <httpRuntime maxRequestLength = "2097151" useFullyQualifiedRedirectUrl="true"/>
  11. </system.web>
  12. */
  13.  
  14. /// <summary>
  15. /// Gets a files formatted size.
  16. /// </summary>
  17. /// <param name="file">The file to return size of.</param>
  18. /// <returns></returns>
  19. //public static string GetFileSize(this FileInfo file)
  20. //{
  21. // try
  22. // {
  23. // //determine all file sizes
  24. // double sizeinbytes = file.Length;
  25. // double sizeinkbytes = Math.Round((sizeinbytes / 1024));
  26. // double sizeinmbytes = Math.Round((sizeinkbytes / 1024));
  27. // double sizeingbytes = Math.Round((sizeinmbytes / 1024));
  28. // if (sizeingbytes > 1)
  29. // return string.Format("{0} GB", sizeingbytes); //returns size in gigabytes
  30. // else if (sizeinmbytes > 1)
  31. // return string.Format("{0} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte
  32. // else if (sizeinkbytes > 1)
  33. // return string.Format("{0} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte
  34. // else
  35. // return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte
  36. // }
  37. // catch { return "Error Getting Size"; } //catches any possible error and just returns error getting size
  38. //}
  39.  
  40. /// <summary>
  41. /// Gets a files formatted size.
  42. /// 获得文件大小
  43. /// 涂聚文
  44. ///
  45. /// Geovin Du
  46. /// </summary>
  47. /// <param name="file"></param>
  48. /// <returns></returns>
  49. public string GetFileSize(double file)
  50. {
  51. try
  52. {
  53. //1.
  54. double byteSize = Math.Round(file / 1024 * 100) * 0.01;
  55. string suffix = "KB";
  56. if (byteSize > 1000)
  57. {
  58. byteSize = Math.Round(byteSize * .001 * 100) * .01;
  59. suffix = "MB";
  60. }
  61. double byteGSize = Math.Round(byteSize / 1024 * 100) * 0.01;
  62. if (byteGSize > 1000)
  63. {
  64. byteGSize = Math.Round(byteGSize * .001 * 100) * .01;
  65. suffix = "GB";
  66. }
  67. return byteSize.ToString() + suffix;
  68.  
  69. //2.
  70. //determine all file sizes
  71. //double sizeinbytes = file;
  72. //double sizeinkbytes = Math.Round((sizeinbytes / 1024));
  73. //double sizeinmbytes = Math.Round((sizeinkbytes / 1024));
  74. //double sizeingbytes = Math.Round((sizeinmbytes / 1024));
  75. //if (sizeingbytes > 1)
  76. // return string.Format("{0:0.00} GB", sizeingbytes); //returns size in gigabytes
  77. //else if (sizeinmbytes > 1)
  78. // return string.Format("{0:0.00} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte
  79. //else if (sizeinkbytes > 1)
  80. // return string.Format("{0:0.00} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte
  81. //else
  82. // return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte
  83. }
  84. catch
  85. {
  86. return "Error Getting Size";
  87. } //catches any possible error and just returns error getting size
  88. }

  

  1. /// <summary>
  2. ///
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. protected void Button1_Click(object sender, EventArgs e)
  7. {
  8. int FileNum = 0;
  9. // Clear the BulletedList.
  10. BulletedList1.Items.Clear();
  11. string filepath = Server.MapPath("./") + "UploadFile";
  12. string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
  13. HttpFileCollection hfc = Request.Files;
  14. //1
  15. for (int i = 0; i < hfc.Count; i++)
  16. {
  17. HttpPostedFile hpf = hfc[i];
  18. if (hfc[i].ContentLength > 0)
  19. {
  20. hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
  21. FileNum++;
  22. //uploadFiles.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
  23. FileUpload1.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
  24. FileUpload2.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
  25. }
  26. else
  27. {
  28. Response.Write("<script>alert('请选择你要上传的文件!')</script>");
  29. //跳出for循环
  30. i = hfc.Count;
  31. break;
  32. }
  33. }
  34. Response.Write("<script>alert('上传成功!')</script>");
  35. Response.Write("共上传成功的文件个数为:" + FileNum + "个");
  36. FileNum = 0;
  37.  
  38. // Check to see if at least one file was specified.
  39. //if (FileUpload1.HasFile | FileUpload2.HasFile)
  40. //{
  41. // Label1.Text = "The file collection consists of:";
  42.  
  43. // // Get the HttpFileCollection.
  44. // //HttpFileCollection hfc = Request.Files;
  45. // foreach (String h in hfc.AllKeys)
  46. // {
  47. // // Add an item to the BulletedList if a file
  48. // // was specified for the corresponding control.
  49. // if (hfc[h].ContentLength > 0)
  50. // {
  51. // BulletedList1.Items.Add(Server.HtmlEncode(hfc[h].FileName));
  52. // }
  53.  
  54. // }
  55. // for (int i = 0; i < hfc.Count; i++)
  56. // {
  57. // filename = DateTime.Now.ToString("yyyyMMddHHmmss");
  58. // HttpPostedFile hpf = hfc[i];
  59. // hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
  60. // }
  61. //}
  62. //else
  63. //{
  64. // Label1.Text = "You did not specify any files to upload or "+ hfc.Count.ToString()+
  65. // "the file(s) could not be found.";
  66. //}
  67. }

  

  1. /// <summary>
  2. ///
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. this.AddFile.Click += new System.EventHandler(this.AddFile_Click);
  9. this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click);
  10. this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick);
  11. }
  12.  
  13. /// <summary>
  14. /// AddFile will add the path of the client side file that is currently in the PostedFile
  15. /// property of the HttpInputFile control to the listbox.
  16. /// </summary>
  17. /// <param name="sender"></param>
  18. /// <param name="e"></param>
  19. private void AddFile_Click(object sender, System.EventArgs e)
  20. {
  21. if (Page.IsPostBack == true)
  22. {
  23. hif.Add(FindFile);
  24. double size = FindFile.PostedFile.ContentLength;
  25. string filesize = GetFileSize(size);
  26. ListBox1.Items.Add(FindFile.PostedFile.FileName + " 文件大小:" + filesize);
  27. // FindFile.PostedFile.ContentLength.ToString();
  28. }
  29. else
  30. {
  31.  
  32. }
  33. }
  34.  
  35. /// <summary>
  36. /// RemvFile will remove the currently selected file from the listbox.
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. private void RemvFile_Click(object sender, System.EventArgs e)
  41. {
  42. if (ListBox1.Items.Count != 0)
  43. {
  44.  
  45. hif.RemoveAt(ListBox1.SelectedIndex);
  46. ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
  47. }
  48.  
  49. }
  50.  
  51. /// <summary>
  52. /// Upload_ServerClick is the server side script that will upload the files to the web server
  53. /// by looping through the files in the listbox.
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. public void Upload_ServerClick(object sender, System.EventArgs e)
  58. {
  59. string baseLocation = Server.MapPath("/upload/");
  60. string status = "";
  61.  
  62. if ((ListBox1.Items.Count == 0) && (filesUploaded == 0))
  63. {
  64. Label1.Text = "Error - a file name must be specified.";
  65. return;
  66.  
  67. }
  68. else
  69. {
  70. foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
  71. {
  72. try
  73. {
  74. string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
  75. HIF.PostedFile.SaveAs(baseLocation + fn);
  76. filesUploaded++;
  77. status += fn + "<br>";
  78. }
  79. catch (Exception err)
  80. {
  81. Label1.Text = "Error saving file " + baseLocation + "<br>" + err.ToString();
  82. }
  83. }
  84.  
  85. if (filesUploaded == hif.Count)
  86. {
  87. Label1.Text = "These " + filesUploaded + " file(s) were uploaded:<br>" + status;
  88. }
  89. hif.Clear();
  90. ListBox1.Items.Clear();
  91. }
  92.  
  93. }

  

csharp: Gets a files formatted size.的更多相关文章

  1. e.target.files[0]层层剖析

    因为我现在拿到的一个功能是上传时过滤掉很大尺寸的图片,所以需要来拿到上传时选择图片的size,所以有了这篇博文 不多说 上代码 $('input').change(function(e){ 1️⃣.c ...

  2. 在使用document.getElementById('xxx').files[0]时,关于计算图片大小

    在使用文件上传属性时,一直好奇图片上传的大小时如何计算的,最近在使用中认识到的计算方式:  首先,图片大小的存储基本单位是字节(byte).每个字节是由8个比特(bit)组成.所以,一个字节在十进制中 ...

  3. Upload Files In ASP.NET Core 1.0 (Form POST And JQuery Ajax)

    Uploading files is a common requirement in web applications. In ASP.NET Core 1.0 uploading files and ...

  4. 7zip File: How to Uncompress 7z files on Ubuntu, Debian, Fedora

    转:http://www.thegeekstuff.com/2010/04/7z-7zip-7za-file-compression/ Question: How do I uncompress a ...

  5. $('xx')[0].files[0]

    ①首先得明白jQuery对象只能使用jQuery对象的属性和方法,JavaScript对象只能使用JavaScript对象的属性和方法: ②files[0]是JavaScript的属性: ③$('xx ...

  6. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  7. [Hive - LanguageManual] Statistics in Hive

    Statistics in Hive Statistics in Hive Motivation Scope Table and Partition Statistics Column Statist ...

  8. Hadoop集群-HDFS集群中大数据运维常用的命令总结

    Hadoop集群-HDFS集群中大数据运维常用的命令总结 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客会简单涉及到滚动编辑,融合镜像文件,目录的空间配额等运维操作简介.话 ...

  9. YASM User Manual

    This document is the user manual for the Yasm assembler. It is intended as both an introduction and ...

随机推荐

  1. [转] Foobar2000 DSP音效外挂元件-Part4

    [转] Foobar2000 DSP音效外挂元件-Part4 在第1部分的文章里主要介绍了foobar2000预设的DSP音效调整,这些则示要介绍几个比较会用到的DSP外挂元件,在foobar2000 ...

  2. datetime 模块

    datetimo 模块和time模块类似,只不过直接帮你定好了格式 import datetime time =datetime.datetime.now() print(time,type(time ...

  3. delphi 10.2 ----简单的递归函数例子求和

    unit Unit10; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  4. 获取指定订阅下所有Azure ARM虚拟机配置(CPU核数,内存大小,磁盘信息)的使用情况

    脚本内容: <# .SYNOPSIS This script grab all ARM VM VHD file in the subscription and caculate VHD size ...

  5. 2016级算法第六次上机-G.ModricWang likes geometry

    1116 ModricWang likes geometry 思路 难题,非常考察几何知识,放在这里作为计算几何场次的最难的题. 原题地址 原版题解 代码

  6. 题目1000:计算a+b

    问题来源 http://ac.jobdu.com/problem.php?pid=1000 问题描述 简单a+b测试,没什么限制,入门级题目. 问题分析 使用scanf.printf或者cin.cou ...

  7. python全栈开发_day13_迭代器和生成器

    一:迭代器 1)可迭代对象 具有内置函数__iter__的数据就是可迭代对象 2)迭代器对象 具有内置函数__next__的数据就是迭代器对象 迭代器对象一定是可迭代对象,可迭代对象不一定是迭代器对象 ...

  8. CF1139D Steps to One 题解【莫比乌斯反演】【枚举】【DP】

    反演套 DP 的好题(不用反演貌似也能做 Description Vivek initially has an empty array \(a\) and some integer constant ...

  9. Linux/Mac安装oh-my-zsh后不执行~/.bash_profile、~/.bashrc解决办法

    安装了zsh之后默认启动执行脚本变为了-/.zshrc. 解决办法: Mac: 修改-/.zshrc文件,在其中添加:source -/.bash_profile.source -/.bashrc:注 ...

  10. [Xamarin] 透過StartActivityForResult傳值回來(转贴)

    上一篇文章(開啟另外一個Activity 並且帶資料),提到了開啟一個新的Activity ,我們將值透過intent 帶到下個Activity 但是,如果我們開啟的Actrivity其實是有一個任務 ...