100求救2005.NET作的大文件上传代码,各位大哥谁有给小弟一份!我从网上下载的好像都缺少类!希望有的给小弟一份!分数不够在加我800多分!只要好用立刻给分!快点阿
解决方案 »
- C#一个用SQL写的问题
- C# gridview中放了dropdownlist控件,关于读取dropdownlist控件ID的问题
- C#绘制曲线图和柱状图,在CSDN下了段代码!不知道怎么用!请各位高手指点!
- 如何将数据库里的image类型数据读取到页面IMAGE控件里?
- NET中,XmlDocument运行时,所需要的.net运行池,怎么下载呢?
- 大家好,请问如果实现(第一条记录、上一条记录、下一条记录最后一条记录)的功能,用什么方法好,dataset吗?有什么比较好的方法
- 方法在new修饰符的作用下是如何调用的(子类对象赋值给父类变量)
- 请教dataset多线程问题
- Windows Form 窗口跳转混乱的问题
- winform程序中如何获取DataGrid当前行改变啊?
- 开源一套我的系统:棋牌游戏大厅
- 分布调试问题:"当前不会命中断点,没有与此命令关联的可执行代码.";
WebForm1.aspx<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CSharpUpload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" enctype="multipart/form-data" runat="server">
<INPUT type=file id=File1 name=File1 runat="server" >
<br>
<input type="submit" id="Submit1" value="Upload" runat="server" NAME="Submit1">
</form>
</body>
</HTML>
WebForm1.aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace CSharpUpload
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load); }
#endregionprivate void Submit1_ServerClick(object sender, System.EventArgs e)
{
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("Data") + "\\" + fn;
try
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write("The file has been uploaded.");
}
catch ( Exception ex )
{
Response.Write("Error: " + ex.Message);
}
}
else
{
Response.Write("Please select a file to upload.");
}
}
}
}
参考:
http://www.codeproject.com/aspnet/fileupload.asp
http://www.c-sharpcorner.com/UploadFile/munnamax/FileUploader02102006073548AM/FileUploader.aspx?ArticleID=c6caab38-acc1-40c6-a5ef-c2ad639bcecb
http://www.15seconds.com/issue/010504.htm
在“服务”里停止IIS admin service 服务。找到Windows\System32\inesrv\下的metabase.xml文件,用记事本打开,找到 ASPMaxRequestEntityAllowed 把它修改为需要的值如102400000(附件大小为100MB)。启动IIS admin service服务。2、改变了web.config中的 maxRequestLength="你的容量"
3、前台代码略,估计有个上传按钮吧,我暂且就认为是 Button1吧
private void Button1_Click(object sender, System.EventArgs e)
{
string clintFileName, FileExt, nFileName;
int i;
if (File1.PostedFile!=null)
{
clintFileName = File1.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引
i = clintFileName.LastIndexOf(".");
//取得文件扩展名
FileExt = clintFileName.Substring(i);
//这里自动根据日期和文件大小不同为文件命名,确保文件名不重复
nFileName = DateTime.Now.DayOfYear.ToString() + File1.PostedFile.ContentLength.ToString();
//保存文件到你所要的目录,这里是IIS根目录下的UDfiles目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
File1.PostedFile.SaveAs(Server.MapPath("UDfiles/" + nFileName + FileExt)) ;
//得到这个文件的相关属性:文件名,文件类型,文件大小 }
}
测试成功
我的代码中有File1就是那个控件 设置为 runat=server
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<INPUT type="file" runat="server" id="File1">
</form>
----------------------------UploadPage.aspx完---------------------------
sing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApp
{
/// <summary>
/// test 的摘要说明。
/// </summary>
public class upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
private void Page_Load(object sender, System.EventArgs e)
{
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
string clintFileName, FileExt, nFileName;
int i;
if (File1.PostedFile!=null)
{
clintFileName = File1.PostedFile.FileName;
i = clintFileName.LastIndexOf(".");
FileExt = clintFileName.Substring(i);
nFileName = DateTime.Now.DayOfYear.ToString() + File1.PostedFile.ContentLength.ToString();
File1.PostedFile.SaveAs(Server.MapPath("UDfiles/" + nFileName + FileExt)) ; }
}
}
}
-------------------------UploadPage.cs部分也完------------------------------
特别是经常要上传,客户多了,服务器的负担更重
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx
<httpRuntime maxRequestLength="500000">
</httpRuntime>
带DEMO和全部源码。