有哪位高手知道ASP.NET中用C#怎样怎样实现文件上传
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有哪位高手知道ASP.NET中用C#怎样怎样实现文件上传相关的知识,希望对你有一定的参考价值。
有哪位高手知道ASP.NET中用C#怎样怎样实现文件上传,急。。。。。。。。。。
说明:这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void bt_upload_Click(object sender, EventArgs e)
try
if (FileUpload1.PostedFile.FileName == "")
this.lb_info.Text = "请选择文件!";
else
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = Server.MapPath("images/") + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
this.lb_info.Text = "上传成功!";
catch (Exception ex)
this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
前台代码:
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
<table style="width: 343px">
<tr>
<td style="width: 100px">
单文件上传</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" Width="475px" />
</td>
<td style="width: 100px">
<asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="上传" /></td>
</tr>
<tr>
<td style="width: 100px; height: 21px;">
<asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label></td>
<td style="width: 100px; height: 21px">
</td>
</tr>
</table>
</form> 参考技术A using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// MyUpload 的摘要说明
/// </summary>
public class MyUpload
private System.Web.HttpPostedFile postedFile = null;
private string savePath = "";
private string[] extension;
private int fileLength = 0;
public string[] filestype = "txt", "doc", "gif" ;
public MyUpload()
//
// TODO: 在此处添加构造函数逻辑
//
public string Help
get
string helpstring;
helpstring = "<font size=3>MyUpload myUpload=new MyUpload(); //构造函数";
helpstring += "myUpload.PostedFile=file1.PostedFile;//设置要上传的文件";
helpstring += "myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\";
helpstring += "myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k";
helpstring += "myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt";
helpstring += "label1.Text=myUpload.Upload();//开始上传,并显示上传结果";
helpstring += "<font size=3 color=red>";
return helpstring;
public System.Web.HttpPostedFile PostedFile
get
return postedFile;
set
postedFile = value;
public string SavePath
get
if (savePath != "") return savePath;
return "c:\\";
set
savePath = HttpContext.Current.Server.MapPath("../upimages/") + value;/////////////////////注意这里选取上传文件路径,可以修改成传递参数类型的
public int FileLength
get
if (fileLength != 0) return fileLength;
return 1024;
set
fileLength = value * 1024;
public string[] Extension
get
if (extension != null) return extension;
return filestype;
set
extension = value;
public string PathToName(string path)
int pos = path.LastIndexOf("\\");
return path.Substring(pos + 1);
public string Upload()
int biaoshi = 0;
if (PostedFile.FileName != "")
try
string fileName = PathToName(PostedFile.FileName);
for (int i = 0; i < Extension.Length; i++)
if (fileName.EndsWith(Extension[i]))
biaoshi = 1;
if (biaoshi != 1)
string errs = "";
for (int i = 0; i < Extension.Length; i++)
errs = errs + Extension[i] + "/";
return "您必须选择" + errs + "类型的文件!";
if (PostedFile.ContentLength > FileLength) return "文件太大,超过了限定值!";
PostedFile.SaveAs(SavePath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4));
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4);
catch (System.Exception exc) return exc.Message;
return "请选择文件后再进行上传!";
////////////////////使用例子,直接复制没有修改/////////////////////
string[] filestype = "gif", "jpg", "png" ;
MyUpload myload = new MyUpload();
myload.PostedFile = File1.PostedFile;
myload.SavePath = "";
myload.FileLength = 1000;
myload.Extension = filestype;
Label1.Text = myload.Upload();
if (Label1.Text.StartsWith("200"))
Label2.Text = "upimages/" + Label1.Text;
Label1.Text = "上传更新成功!";
ASP.NET里在WEB页里怎样做滚动字幕?
我知道有个东西叫marquee。但那在HTML可以这样写,在ASP里就根本打不出这个代码
不要总是说你用HTML写啊,怎么的怎么的,用HTML写我也会。
我现在就是要用ASP里的WEB状态页下写
以上是关于有哪位高手知道ASP.NET中用C#怎样怎样实现文件上传的主要内容,如果未能解决你的问题,请参考以下文章
asp.net里怎样通过下拉框改写Gridview某一列的值
asp.net(C#)怎样将listview中的radiobutton设置成互斥?
我们英语课本附带的光盘只能电脑上看、无法复制操作、哪位高手知道怎样拷贝下来?感激…