.Net neatupload上传控件实现文件上传的进度条

1. 引入bin文件 (可以到neatupload官网下载,也可以到教育厅申报系统中找)

image

2. 将控件加入到工具栏,在工具栏中点鼠标右键,如图:

image

3. 加入neatuplaod这个文件夹(可以到neatupload官网下载,也可以到教育厅申报系统中找)

image

image

image

4. Webconfig的配置(3个地方)

<configSections>配置节下配置:

<!--配置NeatUpload sectionGroup配置节-->

<sectionGroup name="system.web">

<section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler, Brettle.Web.NeatUpload" allowLocation="true" />

</sectionGroup>

<system.web>配置节下配置:

<!--配置NeatUpload neatUpload配置节-->

<neatUpload useHttpModule="True" maxNormalRequestLength="4096" maxRequestLength="2097151" defaultProvider="FilesystemUploadStorageProvider">

<providers>

<add name="FilesystemUploadStorageProvider"

type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload" />

</providers>

</neatUpload>

<httpModules>配置节下配置:

<!--配置NeatUpload httpModules配置节-->

<!--如果不加这httpmodules,进度条不显示-->

<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule, Brettle.Web.NeatUpload"/>

5. 页面代码

<%@ Register Assembly="Brettle.Web.NeatUpload" Namespace="Brettle.Web.NeatUpload"

TagPrefix="Upload" %>

<link href="../../../../NeatUpload/default.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" language="javascript">

function ToggleVisibility(id, type) {

el = document.getElementById(id);

if (el.style) {

if (type == 'on') {

el.style.display = 'block';

}

else {

el.style.display = 'none';

}

}

else {

if (type == 'on') {

el.display = 'block';

}

else {

el.display = 'none';

}

}

}

</script>

<Upload:InputFile ID="AttachFile" runat="server" />

<asp:Button ID="btnAdd" runat="server" Text="上传" OnClientClick="ToggleVisibility('ProgressBar','on')"

OnClick="btnAdd_Click" />

<asp:Label ID="Label10" runat="server" Text="*最大上传为4M" ForeColor="Red"></asp:Label>

<div id="ProgressBar" style="display: none">

<Upload:ProgressBar ID="pbProgressBar" runat='server' Inline="true" Width="800px"

Height="50px" AllowTransparency="False">

</Upload:ProgressBar>

</div>

6. 页面后台代码范例:

protected void btnAdd_Click(object sender, EventArgs e)

{

if (!string.IsNullOrEmpty(AttachFile.FileName))

{

int ProjectID = this.CurrentProjectID;

string FileName = this.AttachFile.FileName;//获取上传文件的文件名

string FileNameExtenter =System.IO.Path.GetExtension(FileName).ToLower(); ;//获取扩展名

if (AttachFile.FileContent.Length > 4096 * 1024 && AttachFile != null)

{

Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.Ticks.ToString(), "<script>alert('文件大于4M,不能上传')</script>");

Stream Sr = AttachFile.FileContent;//创建数据流对象

Sr.Close();

// this.Response.Write("<script language=javascript>alert('文件大于4M,不能上传!');history.go(-1);</script>");

return;

}

if (AttachFile.FileContent.Length == 0)

{

this.Response.Write("<script language=javascript>alert('空文件,不能上传!');history.go(-1);</script>");

Stream Sr = AttachFile.FileContent;//创建数据流对象

Sr.Close();

return;

}

if (AttachFile != null && FileName != null)

{

if (FileNameExtenter == ".doc")

{

Stream Sr = AttachFile.FileContent;//创建数据流对象

int upLength = Convert.ToInt32(AttachFile.ContentLength);

byte[] b = new byte[upLength];//定义byte型数组

Sr.Read(b, 0, upLength); // 数据存放到b数组对象实例中,其中0代表数组指针的起始位置,uplength表示要读取流的长度(指针的结束位置)

Binary Content = new Binary(b);

Attachment _attachment = new Attachment();

_attachment.Entity = "ReportProject";

_attachment.EntityID = ProjectID;

_attachment.Content = Content;

_attachment.FileName = FileName;

_attachment.UsedFlag = 1;

_attachment.Creator = this.CurrentProjectID.ToString();

_attachment.CreateTime = System.DateTime.Now;

_attachment.LastEditor = this.CurrentProjectID.ToString();

_attachment.LastEditTime = System.DateTime.Now;

DataContext.Attachment.InsertOnSubmit(_attachment);

DataContext.SubmitChanges();

this.gv.DataBind();

Sr.Close();

Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.Ticks.ToString(), "<script>alert('附件上传成功!请检查!')</script>");

this.gv.DataBind();

//ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), DateTime.Now.Ticks.ToString(), "<script>window.alert('附件上传成功!');window.location.href=window.location.href;</script>", false);

//ProgressBar.Visible = false;

return;

}

else

{

this.Response.Write("<script language=javascript>alert('不能上传word以外 文件!请先将文件将转换为word(后缀名必须为doc)形式再上传');history.go(-1);</script>");

Stream Sr = AttachFile.FileContent;//创建数据流对象

Sr.Close();

return;

}

}

}

else

{

this.Response.Write("<script language=javascript>alert('请选择上传文件');history.go(-1);</script>");

return;

}

}

//若要上传到本地文件中

//代码:

string path = Server.MapPath("~") + "\File\UpLoads\" + Path.GetFileName(NewUpFile.PostedFile.FileName);

NewUpFile.SaveAs(path);

7. 注意的地方

1>.当需要报错的时候,在报错的函数必须有

Stream Sr = AttachFile.FileContent;//创建数据流对象 Sr.Close();

看似多此一举,但是不写就会有错,

2> . 而且在进度条走完以后后台代码才会执行,故而如果文件过大,待文件上传完毕后提示文件过大会影响用户体验,这个问题待解决

原文地址:https://www.cnblogs.com/lipeng0824/p/4339943.html