web文件上传源码


using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Reflection;

namespace myHttpModule
{
/// <summary>
/// HttpUploadModule 的摘要说明。
/// </summary>
public class HttpUploadModule: IHttpModule
{
 
public HttpUploadModule()
  {
  
//
  
// TODO: 在此处添加构造函数逻辑
  
//
  }
 
public void Init(HttpApplication application)
  {
   application.BeginRequest
+= new EventHandler(this.Application_BeginRequest);
   application.EndRequest
+= new EventHandler(this.Application_EndRequest);
   application.Error
+= new EventHandler(this.Application_Error);
  }

 
public void Dispose()
  {
  }

 
private void Application_BeginRequest(Object sender, EventArgs e)
  {
   HttpApplication app
= sender as HttpApplication;

  
// 如果是文件上传
   if (IsUploadRequest(app.Request))
   {
   
// 返回 HTTP 请求正文已被读取的部分。
    HttpWorkerRequest request = GetWorkerRequest(app.Context);
    Encoding encoding
= app.Context.Request.ContentEncoding;

   
int bytesRead = 0; // 已读数据大小
    int read; // 当前读取的块的大小
    int count = 8192; // 分块大小
    byte[] buffer; // 保存所有上传的数据
    byte[] tempBuff = null;
   
   
if (request != null)
    {
     tempBuff
= request.GetPreloadedEntityBody();
    }
   
if (tempBuff != null)
    {
    
// 获取上传大小
    
//
     long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
    
     buffer
= new byte[length];
     count
= tempBuff.Length; // 分块大小

    
// 将已上传数据复制过去
    
//
     Buffer.BlockCopy(tempBuff, 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);

     
// 表示上传已结束

     }
    }
   }
  }

 
/// <summary>
 
/// 结束请求后移除进度信息
 
/// </summary>
 
/// <param name="sender"></param>
 
/// <param name="e"></param>
  private void Application_EndRequest(Object sender, EventArgs e)
  {

  }

 
/// <summary>
 
/// 如果出错了设置进度信息中状态为“Error”
 
/// </summary>
 
/// <param name="sender"></param>
 
/// <param name="e"></param>
  private void Application_Error(Object sender, EventArgs e)
  {

  }

  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);
   }
  }

 
/// <summary>
 
/// 是否为附件上传
 
/// 判断的根据是ContentType中有无multipart/form-data
 
/// </summary>
 
/// <param name="request"></param>
 
/// <returns></returns>
  bool IsUploadRequest(HttpRequest request)
  {
  
return request.ContentType.ToLower()=="multipart/form-data";
  }

}
}

需要在web.config中加入

  
<httpRuntime executionTimeout="600" maxRequestLength="1048576"/>

<httpModules>

 
<add name="HttpUploadModule" type="myHttpModule" />
</httpModules>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现SpringBoot文件上传的功能,可以参考以下步骤: 1. 使用IDEA创建一个名为springboot-file的SpringBoot项目,将Package name改为com.example.springboot。同时导入Spring Web和Thymeleaf依赖。具体步骤可参考文章《IDEA中创建SpringBoot项目,并实现HelloWorld》的前三个步骤。 2. 下载SpringBoot开发脚手架的源码,该脚手架集合了各种常用框架使用案例,并提供了完善的文档,能够帮助开发者快速搭建基础环境并运行应用。该项目的源码可以在国内的gitee地址或者github地址进行下载。 3. 在src/main/resources文件夹下的application.properties文件中添加文件保存的路径,例如设置为filePath=E:/springboot_save_file/。这样上传的文件将会保存在指定的路径下。 通过参考以上步骤,您可以找到SpringBoot文件上传源码实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot实现文件上传和下载](https://blog.csdn.net/qq_55896432/article/details/127102250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot实战:文件上传下载,代码精简(附git源码)](https://blog.csdn.net/qq_42411805/article/details/124721130)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值