1  /  1  页   1 跳转 查看:589

asp.net上传视频

asp.net上传视频

谁会得或有例子的指教下
 

回复: asp.net上传视频

web上传步骤
1.浏览者选择文件
2.浏览向服务器发送上传请求
3.服务器端接受请求处理
    a.检验文件是否合法,一般只检验后缀名
    b.准备上传文件夹,在配置文件里面设置,必须检测文件夹是否存在。
    c.准备上传路径,上传文件夹的硬盘路径+文件名称(包括后缀名),文件名称可以用时间yyyymmddhhhsss.后缀名
    d.执行上传
asp.net实现
------aspx内容
<aspabel ID="Label1" runat="server" Text="文件"></aspabel>
<asp:FileUpload ID="fu_SelectFile" runat="server" />
<asp:Button ID="btn_UpLoad" runat="server"  Text="上传" />
-----aspx.cs内容
        if (String.IsNullOrEmpty(fu_SelectFile.FileName)) return;//检查是否选择文件
        if (fu_SelectFile.FileContent == null) return;//检查文件是否空
        string strUpLoadFolder = Server.MapPath("~/") + "//MyFile//";//此文件夹本例建在网站根目录
        string strUpLoadFileFullPath = strUpLoadFolder + fu_SelectFile.FileName;//设置上传后的文件路径
        fu_SelectFile.SaveAs(strUpLoadFileFullPath);//文件上传
---配置文件
<system.web>
  <httpRuntime executi maxRequestLength="40960"/>
</system.web>
说明maxRequestLength这个数值单位为K能上传的大小取决于这个值,此时应该中能上传小于4M的文件。
最后编辑顾雪成堆 最后编辑于 2009-08-03 17:01:56
 

asp.net 大文件上传

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Reflection;
//实现IHttpModule接口
      public class HttpUploadModule : IHttpModule
      {
          public HttpUploadModule()
          {
 
          }
 
          public void Init(HttpApplication application)
          {
              //订阅事件
              application.BeginRequest += new EventHandler(this.Application_BeginRequest);
          }
 
          public void Dispose()
          {
          }
 
          private void Application_BeginRequest(Object sender, EventArgs e)
          {
              HttpApplication app = sender as HttpApplication;
              HttpWorkerRequest request = GetWorkerRequest(app.Context);
              Encoding encoding = app.Context.Request.ContentEncoding;
 
              int bytesRead = 0; // 已读数据大小
              int read;          // 当前读取的块的大小
              int count = 8192;  // 分块大小
              byte[] buffer;      // 保存所有上传的数据
 
              if (request != null)
              {
                  // 返回 HTTP 请求正文已被读取的部分。
                  byte[] tempBuff = request.GetPreloadedEntityBody(); //要上传的文件
 
                  // 如果是附件上传
                  if (tempBuff != null && IsUploadRequest(app.Request))    //判断是不是附件上传
                  {
                      // 获取上传大小
                      //
                      long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
                     
                      buffer = new byte[length];
                      count = tempBuff.Length; // 分块大小
 
                      // 将已上传数据复制过去
                      //
                      Buffer.BlockCopy(tempBuff, //源数据
                          0,                      //从0开始读
                          buffer,                //目标容器
                          bytesRead,              //指定存储的开始位置
                          count);                //要复制的字节数。
 
 
                      // 开始记录已上传大小
                      bytesRead = tempBuff.Length;
 
                      // 循环分块读取,直到所有数据读取结束
                      while (request.IsClientConnected() &&!request.IsEntireEntityBodyIsPreloaded() && bytesRead < length)
                      {
                          // 如果最后一块大小小于分块大小,则重新分块
                          if (bytesRead + count > length)
                          {
                              count = (int)(length - bytesRead);
                              tempBuff = new byte[count];
                          }
 
                          // 分块读取
                          read = request.ReadEntityBody(tempBuff, count);
 
                          // 复制已读数据块
                          Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read);
 
                          // 记录已上传大小
                          bytesRead += read;
 
                      }
                      if ( request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() )
                      {
                          // 传入已上传完的数据
                          InjectTextParts(request, buffer);
                      }
                  }
              }
          }
 
 
          HttpWorkerRequest GetWorkerRequest(HttpContext context)
          {
 
              IServiceProvider provider = (IServiceProvider)HttpContext.Current;
              return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
          }
 
          /// <summary>
          /// 传入已上传完的数据
          /// </summary>
          /// <param name="request"></param>
          /// <param name="textParts"></param>
          void InjectTextParts(HttpWorkerRequest request, byte[] textParts)
          {
              BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
 
              Type type = request.GetType();
 
              while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))
              {
                  type = type.BaseType;
              }
 
              if (type != null)
              {
                  type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length);
                  type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length);
                  type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts);
                  type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true);
              }
          }
 
          private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
          {
              return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
          }
 
              /// <summary>
          /// 是否为附件上传
          /// 判断的根据是ContentType中有无multipart/form-data
          /// </summary>
          /// <param name="request"></param>
          /// <returns></returns>
          bool IsUploadRequest(HttpRequest request)
          {
              return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
          }
      }

--页面cs文件中的内容
  protected void Button1_Click(object sender, EventArgs e)
          {
              //要保存的位置
              string strDesPath = "D:\\";
              string strFileName = this.firstFile.PostedFile.FileName;
              strFileName =strDesPath + strFileName.Substring(strFileName.LastIndexOf("\\"));
              //
              this.firstFile.PostedFile.SaveAs(strFileName);
              this.Label1.Text = "文件保存到了:" + strFileName;
          }
 
1  /  1  页   1 跳转

版权所有 bugsbox技术论坛 
苏ICP备09047609号  我要啦免费统计 技术支持 闪屏 Address:BinHuArea,City:WuXi,Province:JiangSu,Zip Code:214000,Country:China,E-Mail:bugsbox@163.com 网址专家互链  Sitemap

Powered by Discuz!NT 2.0.1214 (Licensed)    Copyright © 2001-2010 Comsenz Inc.
Processed in 0.21875 second(s) , 3 queries. 苏ICP备09047609号
返顶部