博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Net下载文件

Posted on 2010-05-31 16:34  gzlxm  阅读(203)  评论(0)    收藏  举报

文件下载:

       string filepath = Server.MapPath("~/public/file.txt"); 
        FileInfo fi = new FileInfo(filepath);
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
        Response.AddHeader("Content-Length", fi.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.Filter.Close();
        Response.WriteFile(fi.FullName);
        Response.End();

 

 

.net后台获取html控件值

方法1:

c#:  Label1.Text = Request.Form["txtName"].ToString();

vb.net: Request.Form("txtName").ToString()

方法2:

C#:System.Collections.Specialized.NameValueCollection nc = new System.Collections.Specialized.NameValueCollection(Request.Form);Label1.Text = nc.GetValues("txtName")[0].ToString();

注: "txtName"为Html控件的"name"属性值

 

 

.net(C#)时间相减、C#计算时间间隔

/// <summary>
        /// 计算两个日期的时间间隔
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <remarks>Add: cuiwk 2010-05-31</remarks>
        /// <returns>时间间隔</returns>
        public string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;

            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            dateDiff = ts.Days.ToString() + "天" +
                   ts.Hours.ToString() + "小时" +
                  ts.Minutes.ToString() + "分钟" +
                  ts.Seconds.ToString() + "秒";

            return dateDiff;
        }

    }

说明:
1.DateTime值类型代表了一个从公元0001年1月1日0点0分0秒到公元9999年12月31日23点59分59秒之间的具体日期时刻。因此,你可以用DateTime值类型来描述任何在想象范围之内的时间。一个DateTime值代表了一个具体的时刻
2.TimeSpan值包含了许多属性与方法,用于访问或处理一个TimeSpan值
下面的列表涵盖了其中的一部分:
Add:与另一个TimeSpan值相加。
Days:返回用天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
Hours:返回用小时计算的TimeSpan值
Milliseconds:返回用毫秒计算的TimeSpan值。
Minutes:返回用分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回用秒计算的TimeSpan值。
Subtract:从中减去另一个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表示的天数。
TotalHours:返回TimeSpan值表示的小时数。
TotalMilliseconds:返回TimeSpan值表示的毫秒数。
TotalMinutes:返回TimeSpan值表示的分钟数。
TotalSeconds:返回TimeSpan值表示的秒数。