一切福田,不離方寸,從心而覓,感無不通。

Category Archives: Asp.net

利用InputStream 属性直接从HttpPostedFile对象读取文本内容

//利用InputStream 属性直接从HttpPostedFile对象读取文本内容 System.IO.Stream MyStream; int FileLen; FileLen = file.ContentLength; // 读取文件的 byte[] byte[] bytes = new byte[FileLen]; MyStream = file.InputStream; MyStream.Read(bytes, 0, FileLen); from:http://blog.csdn.net/yyixin/article/details/5336899

龙生   27 Aug 2014
View Details

WebRequest 对象的使用

from:http://www.cnblogs.com/haogj/archive/2011/06/09/2076708.html

龙生   27 Aug 2014
View Details

使用HttpWebRequest实现大文件上传

Author:xuzhihong Create Date:2011-06-03 Descriptions: WinForm程序使用HttpWebRequest实现大文件上传 概述: 通常在WinForm程序中都是采用WebClient方式实现文件上传功能,本身这个方式没有问题,但是当需要上传大文件比如说(300+M)的时候,那么WebClient将会报内存不足异常(Out of Memory Exceptions),究其原因是因为WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器。本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述将如何将“文件上传”功能做为用户自定义控件,实现模块重用。   关键词:HttpWebRequest、WebClient、OutOfMemoryExceptions   解决方案: 开始我在WinForm项目中实现文件上传功能的时候,是采用WebClient(WebClient myWebClient = new WebClient();)方式,这大部分情况都是正确的,但有时候会出现内存不足的异常(Out of Memory Exceptions),经常测试,发现是由于上传大文件的时候才导致这问题。在网上查阅了一下其他网友的解决方案,最后找的发生异常的原因:“WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器”,详细请参考:http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx 。按照这个解释,那么大文件上传出现内存不足的异常也就不足为奇了。下面我将讲述如何一步步使用HttpWebRequest方式来实现文件分块上传数据流至服务器。 按照惯例还是先预览一下文件上传最后的效果吧,如下图所示:   界面分为两部分,上面是文件基本信息,下面是文件上传自定义控件,我这里实现的是一个案件上传多个监控视频功能。以下是详细步骤: 第一步:创建用户自定义控件BigFileUpload.xaml 文件上传是一个非常常用的功能,为了所写的程序能非常方便地多次重复使用,我决定将其处理为一个用户自定义控件(UserControl)。 我们先在项目中创建一个FileUpload文件夹,在其目录下新建一个WPF自定义控件文件命名为BigFileUpload.xaml,这样就表示文件上传是一个独立的小模块使用。之所以用WPF自定义控件是因为WPF页面效果好看点,而且我想以后可能大部分C/S程序都会渐渐的由WinForm转向WPF吧,当然创建Window Forms用户控件也是没有问题的。然后我们需要做一个下图效果的页面布局:   前台设计代码如下: <UserControl x:Class="CHVM.FileUpload.BigFileUpload" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="160" Width="480"> <Grid Height="160" Width="480" Background="White"> <Label Height="28" HorizontalAlignment="Left" Margin="16,10,0,0" Name="label1" VerticalAlignment="Top" Width="53">文件</Label> <Label HorizontalAlignment="Left" Margin="15,52,0,80" Name="label2" Width="54">进度</Label> <ProgressBar Height="20" Margin="61,52,116,0" Name="progressBar1" VerticalAlignment="Top" /> <TextBox Height="23" Margin="61,12,116,0" Name="txtBoxFileName" VerticalAlignment="Top" /> <Button Height="23" HorizontalAlignment="Right" Margin="0,10,35,0" Name="BtnBrowse" VerticalAlignment="Top" Width="75" Click="BtnBrowse_Click">浏览…</Button> <Button Height="23" HorizontalAlignment="Right" Margin="0,52,35,0" Name="BtnUpload" VerticalAlignment="Top" Width="75" Click="BtnUpload_Click">上传</Button> <Label HorizontalAlignment="Left" Margin="16,0,0,44" Name="lblState" Width="183" Height="35" […]

龙生   27 Aug 2014
View Details

asp.net中使用HttpWebRequest发送上传文件

一个网站中需要上传一个文件到另一个网站,可以使用HttpWebRequest或者WebClient。 但是WebClient需要首先上传文件到服务器,才能执行发送,不太符合我的需求,这里不再介绍。 通过HttpWebRequest发送的原理: 构建一个HttpWebRequest,通过FileUpload获取要上传的文件,通过字节流发送这个文件,另一个网站接收字节流,保存到服务器。 发送程序: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 //获取要上传的文件信息        byte[]data=fileupload1.FileBytes;        stringfileName=fileupload1.FileName;        stringfileType=fileupload1.PostedFile.ContentType;        stringfileSize=data.Length.ToString();        HttpWebRequest myRequest=(HttpWebRequest)WebRequest.Create("http://localhost:8102/Default.aspx");        myRequest.Method="POST";        myRequest.ContentType=fileType;        myRequest.ContentLength=data.Length;        myRequest.Headers.Add("FileType",Server.UrlEncode(fileType));        myRequest.Headers.Add("FileSize",fileSize);        myRequest.Headers.Add("FileName",Server.UrlEncode(fileName));        using(Stream newStream=myRequest.GetRequestStream())        {            // Send the data.            newStream.Write(data,0,data.Length);            newStream.Close();        }        // Get response        HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();        StreamReader reader=newStreamReader(myResponse.GetResponseStream(),Encoding.UTF8);        stringcontent=reader.ReadToEnd(); 接收程序: 0 1 2 3 4 5 6 7 8 9 10 stringfileName=Server.UrlDecode(Request.Headers["FileName"].ToString());            stringfileType=Server.UrlDecode(Request.Headers["FileType"].ToString());            intfileSize=int.Parse(Request.Headers["FileSize"].ToString());            byte[]bytes=Request.BinaryRead(fileSize);            File.WriteAllBytes(Server.MapPath("~/uploadfiles/"+fileName),bytes);            Response.HeaderEncoding=System.Text.Encoding.UTF8;            Response.Charset="utf-8";            Response.Write("FileType:"+fileType+";FileName:"+fileName+";FileSize:"+fileSize); FROM:http://blog.bossma.cn/dotnet/asp-net-httpwebrequest-upload-send-file/

龙生   27 Aug 2014
View Details

ASP.NET实现图片防盗链

使用httpHandle来实现,对图片文件的请求做专门的处理 第一步:创建一个类,继承自IHttpHandler,代码如下 C# code using System; using System.Web; namespace CustomHandler{ public class JpgHandler : IHttpHandler{ public void ProcessRequest(HttpContext context){ // 获取文件服务器端物理路径 string FileName = context.Server.MapPath(context.Request.FilePath); // 如果UrlReferrer为空,则显示一张默认的禁止盗链的图片 if (context.Request.UrlReferrer.Host == null){ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/error.jpg"); }else{ // 如果 UrlReferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片 if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); }else{ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/error.jpg"); } } } public bool IsReusable{ get{ return true; } } } } 第二步:编译成DLL csc /t:library CustomHandler.cs 第三步:添加编译好的DLL引用到当前站点的bin文件夹下 第四步:在Web.Config 中注册这个Handler C# code <system.web> <httpHandlers> <add path="*.jpg,*.jpeg,*.gif,*.png,*.bmp" verb="*" type="CustomHandler.JpgHandler,CustomHandler" /> </httpHandlers> </system.web> //verb指的是请求此文件的方式,可以是post或get,用*代表所有访问方式。CustomHandler.JpgHandler表示命名空间和类名,CustomHandler表示程序集名。 from:http://www.cnblogs.com/ghfsusan/archive/2011/02/25/1964579.html

龙生   25 Jul 2014
View Details

Global.asax的Application_BeginRequest实现url重写无后缀的代码

利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 <%@ Application Language="C#" %> <script RunAt="server"> void Application_BeginRequest(object sender, EventArgs e) { string oldUrl = System.Web.HttpContext.Current.Request.RawUrl; //获取初始url //~/123.aspx → ~/Index.aspx?id=123 Regex reg = new Regex(@"^\/\d+\.html"); if (reg.IsMatch(oldUrl)) { string id = reg.Match(oldUrl).ToString().Substring(1, reg.Match(oldUrl).ToString().LastIndexOf(".") – 1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/123 → ~/Index.aspx?id=123 Regex reg1 = new Regex(@"^\/\d+$"); if (reg1.IsMatch(oldUrl)) { string id = reg1.Match(oldUrl).ToString().Substring(1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/index/123 → ~/Index.aspx?id=123 Regex reg3 = new Regex(@"^\/index\/\d+$"); if (reg3.IsMatch(oldUrl)) { string id = reg3.Match(oldUrl).ToString().Substring(7); Context.RewritePath("~/Index.aspx?id=" + id); } } </script> from:http://www.jb51.net/article/40587.htm

龙生   25 Jul 2014
View Details

ManualResetEvent详解

1. 源码下载: 下载地址:http://files.cnblogs.com/tianzhiliang/ManualResetEventDemo.rar Demo: 2. ManualResetEvent详解 ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态,此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态(即对 WaitOne 的调用的线程将立即返回,并不阻塞),直到它被手动重置。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。   using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ManualResetEventDemo {     class MREDemo     {         private ManualResetEvent _mre;         public MREDemo()         {             this._mre = new ManualResetEvent(true);         }         public void CreateThreads()         {             Thread t1 = new Thread(new ThreadStart(Run));             t1.Start();             Thread t2 = new Thread(new ThreadStart(Run));             t2.Start();         }         public void Set()         {             this._mre.Set();         }         public void Reset()         {             this._mre.Reset();         }         private void Run()         {             string strThreadID = string.Empty;             try             {                 while (true)                 {                     // 阻塞当前线程                     this._mre.WaitOne();                     strThreadID = Thread.CurrentThread.ManagedThreadId.ToString();                     Console.WriteLine("Thread(" + strThreadID + ") is running…");                     Thread.Sleep(5000);                 } […]

龙生   30 Jun 2014
View Details

浅谈ThreadPool 线程池

相关概念:     线程池可以看做容纳线程的容器;     一个应用程序最多只能有一个线程池;     ThreadPool静态类通过QueueUserWorkItem()方法将工作函数排入线程池;     每排入一个工作函数,就相当于请求创建一个线程; 线程池的作用: 线程池是为突然大量爆发的线程设计的,通过有限的几个固定线程为大量的操作服务,减少了创建和销毁线程所需的时间,从而提高效率。 如果一个线程的时间非常长,就没必要用线程池了(不是不能作长时间操作,而是不宜。),况且我们还不能控制线程池中线程的开始、挂起、和中止。 什么时候使用ThreadPool? ThreadPool 示例一 : ThreadPool_1.cs using System; using System.Text; using System.Threading; namespace 多线程 { public class Example { public static void Main() { // Queue the task.             ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); Console.WriteLine("Main thread does some work, then sleeps."); Thread.Sleep(1000); Console.WriteLine("Main thread exits."); } static void ThreadProc(Object stateInfo) { // No state object was passed to QueueUserWorkItem, // so stateInfo is null.             Console.WriteLine("Hello from the thread pool."); } } } ThreadPool 示例二 : ThreadPool_2.cs using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace CS_Test { class ThreadPool_Demo { // 用于保存每个线程的计算结果         static int[] result = new int[10]; //注意:由于WaitCallback委托的声明带有参数, //      所以将被调用的Fun方法必须带有参数,即:Fun(object obj)。         static void Fun(object obj) { int n = (int)obj; //计算阶乘             int fac = 1; for (int i = 1; i <= n; i++) { fac *= i; } //保存结果             result[n] = fac; } static void Main(string[] args) { //向线程池中排入9个工作线程             for (int i = 1; i <= 9 ; i++) { //QueueUserWorkItem()方法:将工作任务排入线程池。                 ThreadPool.QueueUserWorkItem(new WaitCallback(Fun),i); // Fun 表示要执行的方法(与WaitCallback委托的声明必须一致)。 // i   为传递给Fun方法的参数(obj将接受)。             } //输出计算结果             for (int i = 1; i <= 9; i++) { Console.WriteLine("线程{0}: {0}! = {1}",i,result[i]); } } } } ThreadPool的作用: 参考来源: C#多线程学习(四) 多线程的自动管理(线程池)  [叩响C#之门]写给初学者:多线程系列( 十一)——线程池(ThreadPool) from:http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html

龙生   30 Jun 2014
View Details

lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)

//封装类   [csharp] view plaincopyprint? using System;   using System.Collections.Generic;   using System.Linq;   using System.Web;   using Lucene.Net.Analysis;   using Lucene.Net.Index;   using Lucene.Net.Documents;   using System.Reflection;   using Lucene.Net.QueryParsers;   using Lucene.Net.Search;   namespace SearchTest   {       /// <summary>       /// 盘古分词在lucene.net中的使用帮助类       /// 调用PanGuLuceneHelper.instance       /// </summary>       public class PanGuLuceneHelper       {           private PanGuLuceneHelper() { }             #region 单一实例           private static PanGuLuceneHelper _instance = null;           /// <summary>           /// 单一实例           /// </summary>           public static PanGuLuceneHelper instance           {               get               {                   if (_instance == null) _instance = new PanGuLuceneHelper();                   return _instance;               }           }           #endregion             #region 分词测试           /// <summary>           /// 分词测试           /// </summary>           /// <param name="keyword"></param>           /// <returns></returns>           public string Token(string keyword)           {               string ret = "";               System.IO.StringReader reader = new System.IO.StringReader(keyword);               Lucene.Net.Analysis.TokenStream ts = analyzer.TokenStream(keyword, reader);               bool hasNext = ts.IncrementToken();               Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;               while (hasNext)               {                   ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();                   ret += ita.Term + "|";                   hasNext = ts.IncrementToken();               }               ts.CloneAttributes();               reader.Close();               analyzer.Close();               return ret;           }           #endregion             #region 创建索引           /// <summary>           /// 创建索引           /// </summary>           /// <param name="datalist"></param>           /// <returns></returns>           public bool CreateIndex(List<MySearchUnit> datalist)           {               IndexWriter writer = null;               try               {                   writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)               }               catch               {                   writer = new IndexWriter(directory_luce, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)               }               foreach (MySearchUnit data in datalist)               {                   CreateIndex(writer, data);               }               writer.Optimize();   […]

龙生   27 Jun 2014
View Details
1 32 33 34 48