分享
 
 
 

csharp: Gets a files formatted size.

王朝学院·作者佚名  2016-05-20
窄屏简体版  字體: |||超大  

csharp: Gets a files formatted size. /* asp.net 默认上传文件是4M ,可以修改服务配置.. <system.web> <!-- 指示 ASP.NET 支持的最大文件上载大小。 该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。 指定的大小以 KB 为单位。默认值为 4096 KB (4 MB)。最大为:2097151 KB 此处改为40M大小的文件上传限制。 --> <httPRuntime maxRequestLength = "2097151" useFullyQualifiedRedirectUrl="true"/> </system.web> */ /// <summary> /// Gets a files formatted size. /// </summary> /// <param name="file">The file to return size of.</param> /// <returns></returns> //public static string GetFileSize(this FileInfo file) //{ // try // { // //determine all file sizes // double sizeinbytes = file.Length; // double sizeinkbytes = Math.Round((sizeinbytes / 1024)); // double sizeinmbytes = Math.Round((sizeinkbytes / 1024)); // double sizeingbytes = Math.Round((sizeinmbytes / 1024)); // if (sizeingbytes > 1) // return string.Format("{0} GB", sizeingbytes); //returns size in gigabytes // else if (sizeinmbytes > 1) // return string.Format("{0} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte // else if (sizeinkbytes > 1) // return string.Format("{0} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte // else // return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte // } // catch { return "Error Getting Size"; } //catches any possible error and just returns error getting size //} /// <summary> /// Gets a files formatted size. /// 获得文件大小 /// 涂聚文 /// /// Geovin Du /// </summary> /// <param name="file"></param> /// <returns></returns> public string GetFileSize(double file) { try { //1. double byteSize = Math.Round(file / 1024 * 100) * 0.01; string suffix = "KB"; if (byteSize > 1000) { byteSize = Math.Round(byteSize * .001 * 100) * .01; suffix = "MB"; } double byteGSize = Math.Round(byteSize / 1024 * 100) * 0.01; if (byteGSize > 1000) { byteGSize = Math.Round(byteGSize * .001 * 100) * .01; suffix = "GB"; } return byteSize.ToString() + suffix; //2. //determine all file sizes //double sizeinbytes = file; //double sizeinkbytes = Math.Round((sizeinbytes / 1024)); //double sizeinmbytes = Math.Round((sizeinkbytes / 1024)); //double sizeingbytes = Math.Round((sizeinmbytes / 1024)); //if (sizeingbytes > 1) // return string.Format("{0:0.00} GB", sizeingbytes); //returns size in gigabytes //else if (sizeinmbytes > 1) // return string.Format("{0:0.00} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte //else if (sizeinkbytes > 1) // return string.Format("{0:0.00} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte //else // return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte } catch { return "Error Getting Size"; } //catches any possible error and just returns error getting size }

/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { int FileNum = 0; // Clear the BulletedList. BulletedList1.Items.Clear(); string filepath = Server.MapPath("./") + "UploadFile"; string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); HttpFileCollection hfc = Request.Files; //1 for (int i = 0; i < hfc.Count; i++) { HttpPostedFile hpf = hfc[i]; if (hfc[i].ContentLength > 0) { hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName)); FileNum++; //uploadFiles.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName)); FileUpload1.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName)); FileUpload2.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName)); } else { Response.Write("<script>alert('请选择你要上传的文件!')</script>"); //跳出for循环 i = hfc.Count; break; } } Response.Write("<script>alert('上传成功!')</script>"); Response.Write("共上传成功的文件个数为:" + FileNum + "个"); FileNum = 0; // Check to see if at least one file was specified. //if (FileUpload1.HasFile | FileUpload2.HasFile) //{ // Label1.Text = "The file collection consists of:"; // // Get the HttpFileCollection. // //HttpFileCollection hfc = Request.Files; // foreach (String h in hfc.AllKeys) // { // // Add an item to the BulletedList if a file // // was specified for the corresponding control. // if (hfc[h].ContentLength > 0) // { // BulletedList1.Items.Add(Server.HtmlEncode(hfc[h].FileName)); // } // } // for (int i = 0; i < hfc.Count; i++) // { // filename = DateTime.Now.ToString("yyyyMMddHHmmss"); // HttpPostedFile hpf = hfc[i]; // hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName)); // } //} //else //{ // Label1.Text = "You did not specify any files to upload or "+ hfc.Count.ToString()+ // "the file(s) could not be found."; //} }

/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { this.AddFile.Click += new System.EventHandler(this.AddFile_Click); this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click); this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick); } /// <summary> /// AddFile will add the path of the client side file that is currently in the PostedFile /// property of the HttpInputFile control to the listbox. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddFile_Click(object sender, System.EventArgs e) { if (Page.IsPostBack == true) { hif.Add(FindFile); double size = FindFile.PostedFile.ContentLength; string filesize = GetFileSize(size); ListBox1.Items.Add(FindFile.PostedFile.FileName + " 文件大小:" + filesize); // FindFile.PostedFile.Conten

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有