当前位置:Gyms

web开发《繁:發》步骤

2025-02-26 04:36:25Gyms

求asp.net大文件分块上传实例源码?一般的在Asp.net里上传文件都是10m左右,要做到大文件上传,必须要改web.config,不过改了web.config有时候也上传不成功,那是每次上传的文件太大,浏览器在这个过程中会超时,采用分块上传的方法就可以避免这种情况

求asp.net大文件分块上传实例源码?

一般的在Asp.net里上传文件都是10m左右,要做到大文件上传,必须要改web.config,不过改了web.config有时候也上传不成功,那是每次上传的文件太大,浏览器在这个过程中会超时,采用分块上传的方法就可以避免这种情况。

2.分块上传就是利用post的方法,把数据分块(繁:塊)上传,每块上传的数据量少,不会引起超时的问题。不说了,看{拼音:kàn}代码吧。

Code

1 //实现IHttpModule接口kǒu

2 public class HttpUploadModule : IHttpModule

3 {

4 public HttpUploadModule()

5 {

6

7 }

澳门威尼斯人

9 public void Init(HttpApplication application)

10 {

11 //订阅事(读:shì)件

12 application.BeginRequest = new EventHandler(this.Application_BeginRequest)

13 }

14

15 public void Dispose()

16 {

17 }

18

19 private void Application_BeginRequest(Object sender, EventArgs e)

20 {

21 HttpApplication app = sender as HttpApplication

开云体育

22 HttpWorkerRequest request = GetWorkerRequest(app.Context)

23 Encoding encoding = app.Context.Request.ContentEncoding

24

25 int bytesRead = 0 // 已读数据大[dà]小

26 int read // 当前读取的块的大小(读:xiǎo)

27 int count = 8192 // 分(拼音:fēn)块大小

28 byte[] buffer // 保存所有上传的数据(拼音:jù)

29

30 if (request != null)

31 {

32 // 返回 HTTP 请求正文(拼音:wén)已被读取的部分。

33 byte[] tempBuff = request.GetPreloadedEntityBody() //要上传的文件(pinyin:jiàn)

娱乐城

34

35 // 如果【guǒ】是附件上传

36 if (tempBuff != null && IsUploadRequest(app.Request)) //判断是《pinyin:shì》不是附件上传[繁:傳]

37 {

38 // 获取上传大小(pinyin:xiǎo)

39 //

40 long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength))

41

42 buffer = new byte[length]

43 count = tempBuff.Length // 分块大小【xiǎo】

44

直播吧

45 // 将已上传数《繁体:數》据复制过去

46 //

47 Buffer.BlockCopy(tempBuff, //源【练:yuán】数据

48 0, //从0开始{shǐ}读

49 buffer, //目{pinyin:mù}标容器

50 bytesRead, //指定dìng 存储的开始位置

51 count) //要复制(繁:製)的字节数。

52

53

54 // 开始记录已上(shàng)传大小

55 bytesRead = tempBuff.Length

56

57 // 循环分块读取,直【zhí】到所有数据读取结束

58 while (request.IsClientConnected() &&!request.IsEntireEntityBodyIsPreloaded() && bytesRead < length)

59 {

60 // 如果最后一块(繁体:塊)大小小于分块大小,则重新分块

61 if (bytesRead count > length)

62 {

63 count = (int)(length - bytesRead)

64 tempBuff = new byte[count]

65 }

66

67 // 分块读《繁:讀》取

68 read = request.ReadEntityBody(tempBuff, count)

69

70 // 复制(繁:製)已读数据块

71 Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read)

72

73 // 记[拼音:jì]录已上传大小

74 bytesRead = read

75

76 }

77 if ( request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() )

78 {

79 // 传入已上传完的数(繁体:數)据

80 InjectTextParts(request, buffer)

81 }

82 }

83 }

84 }

85

86

87 HttpWorkerRequest GetWorkerRequest(HttpContext context)

88 {

89

90 IServiceProvider provider = (IServiceProvider)HttpContext.Current

91 return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest))

92 }

93

94 ///

95 /// 传入已上传(繁:傳)完的数据

96 ///

97 ///

澳门永利

99 void InjectTextParts(HttpWorkerRequest request, byte[] textParts)

100 {

101 BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic

102

103 Type type = request.GetType()

104

105 while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))

106 {

107 type = type.BaseType

108 }

109

110 if (type != null)

111 {

112 type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length)

113 type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length)

114 type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts)

115 type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true)

116 }

117 }

118

119 private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)

120 {

121 return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0)

122 }

123

124 ///

125 /// 是否{拼音:fǒu}为附件上传

126 /// 判断的根据[繁:據]是ContentType中有无multipart/form-data

127 ///

128 ///

129 ///

130 bool IsUploadRequest(HttpRequest request)

131 {

132 return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data")

133 }

134 }

3.用【读:yòng】法

(1)修改【读:gǎi】web.config

1

2

3 type="HttpModelApp.HttpUploadModule, HttpModelApp" />

4

5

6

幸运飞艇

8 executionTimeout="300"

9 /> (2)aspx

1

2

3

4

5

6

(3)aspx.cs

娱乐城

2 {

3 //要保[读:bǎo]存的位置

4 string strDesPath = "D:\"

娱乐城

5 string strFileName = this.firstFile.PostedFile.FileName

6 strFileName =strDesPath strFileName.Substring(strFileName.LastIndexOf("\"))

7 //

8 this.firstFile.PostedFile.SaveAs(strFileName)

9 this.Label1.Text = "文件保存(读:cún)到了:" strFileName

10 }

4.大文件上传的【拼音:de】限制

虽然可以上传大文件,但是这(繁体:這)个大小也是有限制的,不能超过2G的大小。

还有啊,大家不要忘记了导入命{拼音:mìng}名空间.

using System

using System.Collections

using System.Collections.Specialized

using System.Globalization

using System.IO

using System.Text

using System.Web

using System.Reflection

web.config文(练:wén)件如下

澳门巴黎人

华体会体育

type="HttpModelApp.HttpUploadModule, HttpModelApp" />

maxRequestLength="2000000"

executionTimeout="300"

/>

本文链接:http://21taiyang.com/Gyms/1789616.html
web开发《繁:發》步骤转载请注明出处来源