/*
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.ContentLength.ToString();
}
else
{ }
} /// <summary>
/// RemvFile will remove the currently selected file from the listbox.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RemvFile_Click(object sender, System.EventArgs e)
{
if (ListBox1.Items.Count != 0)
{ hif.RemoveAt(ListBox1.SelectedIndex);
ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
} } /// <summary>
/// Upload_ServerClick is the server side script that will upload the files to the web server
/// by looping through the files in the listbox.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Upload_ServerClick(object sender, System.EventArgs e)
{
string baseLocation = Server.MapPath("/upload/");
string status = ""; if ((ListBox1.Items.Count == 0) && (filesUploaded == 0))
{
Label1.Text = "Error - a file name must be specified.";
return; }
else
{
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
HIF.PostedFile.SaveAs(baseLocation + fn);
filesUploaded++;
status += fn + "<br>";
}
catch (Exception err)
{
Label1.Text = "Error saving file " + baseLocation + "<br>" + err.ToString();
}
} if (filesUploaded == hif.Count)
{
Label1.Text = "These " + filesUploaded + " file(s) were uploaded:<br>" + status;
}
hif.Clear();
ListBox1.Items.Clear();
} }

  

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. Microsoft Windows XP Professional X64 Edition Corporate Keys

    FVMK4-6DD4B-26MB4-74JB2-R4XWM DHR8W-69GX3-YWPM9-P98K2-B2V4Y DDR6D-XMQ6V-78Y2B-B6TP4-YXMRY J4K6H-DTTF ...

  2. mxonline实战12, 课程评论,相关课程推荐,课程视频页

    对应github地址:第12天   一. 课程评论   1. 创建URL, VIEW courses/views.py -> Course

  3. Laravel 完整开源项目大全

    来自 Laravel学院:http://laravelacademy.org/ http://laravelacademy.org/laravel-project 原型项目 Laravel 5 Boi ...

  4. 使用sourceTree需要注意的地方

    1.使用CocoaPods 管理第三方库的时候,需要注意不要把Pod文件夹上传到版本管理服务器中 2.使用xcdoe的时候,还有一些个人用户数据也不要上传,可有效避免冲突的发生频率 3.团队开发的时候 ...

  5. leetcode-39-组合总和(有趣的递归)

    题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...

  6. Asp.net的生命周期之应用程序生命周期

    参考:http://msdn.microsoft.com/zh-cn/library/ms178473(v=vs.100).aspx 参考:http://www.cnblogs.com/JimmyZh ...

  7. CSS02--四种样式、背景、文本、链接状态、表格样式

    接上面的“CSS01”,我们接着来说一下样式.很多人不知道的是一个HTML元素有四种样式,分别是浏览器默认样式.外部样式.内部样式.内联样式,而它们的优先级是越来越高的,后面的样式会覆盖前面的样式.多 ...

  8. JDK7,8,JD9的hashmap,hashtable,concurrenthashmap及他们的区别

    1:hashmap简介(如下,数组-链表形式) HashMap的存储结构 图中,紫色部分即代表哈希表,也称为哈希数组(默认数组大小是16,每对key-value键值对其实是存在map的内部类entry ...

  9. 本地docker镜像上传Docker Hub,并且在腾讯云上pull该镜像,最后运行成功。

    1:在docker hub 上注册一个账号(本人直接能注册,有的说不能),然后创建自己的仓库. 2:登录docker hub ( longdbdocker --hub账号,longdb --仓库名称) ...

  10. Mac 10.12常用软件清单

    链接: https://pan.baidu.com/s/1slds1OD 密码: 7m5t 配套教程:http://www.cnblogs.com/EasonJim/tag/mac/ 如果失效了,联系 ...