天天看点

C#实现Web文件上传的两种方法

1. c#实现web文件的上传

  在web编程中,我们常需要把一些本地文件上传到web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛。

  那么使用c#如何实现文件上传的功能呢?下面笔者简要介绍一下。

  首先,在你的visual c# web project 中增加一个上传用的web form,为了要上传文件,需要在toolbox中选择html类的file field控件,将此控件加入到web form中,然而此时该控件还不是服务端控件,我们需要为它加上如下一段代码:<input id=previousfile1 type=file size=49 runat="server">,这样它就成为服务端控件了,如果需要同时上传数个文件时,我们可以相应增加此控件。

  需要注意的是代码中一定要把<form>的属性设置成为:

<form method=post enctype=multipart/ form-data runat="server">

  如果没有这个属性,就不能实现上传。

  然后在此web form中增加一个web form类的button,双击button添加如下代码:

//上传图片的程序段

datetime now = datetime.now ;

//取现在时间到datatime类的对象now中

string strbaselocation = "d:\web\fc\pic\";

//这是文件将上传到的服务器的绝对目录

if (previousfile1.postedfile.contentlength != 0) //判断选取对话框选取的文件长度是否为0

{

    previousfile1.postedfile.saveas(strbaselocation+now.dayofyear.tostring()+previousfile1.postedfile.contentlength.tostring()+".jpg");

//执行上传,并自动根据日期和文件大小不同为文件命名,确保不重复

label1.text="图片1已经上传,文件名为:"+now.dayofyear.tostring()+previousfile1.postedfile.contentlength.tostring()+".jpg";

 navigator.insert(system.xml.treeposition.after, xmlnodetype.element,"pic1","","") ;

    navigator.insert(system.xml.treeposition.firstchild, xmlnodetype.text,"pic1","","") ;

    navigator.value= now.dayofyear.tostring()+previousfile1.postedfile.contentlength.tostring()+".jpg" ;

    navigator.movetoparent() ;

}

  上面的代码用于笔者开发的一个使用xml文件存储新闻信息的系统中,后面几句代码作用是写上传文件信息到xml文件中。如果要上传其他类型文件,只需要将jpg改为相应类型的后缀名即可,如改为doc即可上传word文件,浏览器即可直接浏览上传的word文件。

  【注意事项】

  1. 上传文件不可以无限大;

  2. 要注意iis的安全性方面的配合;

  3. 用visual studio 的安装项目做安装程序的时候,请注意安装程序所在的绝对路径问题;

  4. 注意文件上传后的重名问题。

2. c#实现web文件的上传

using system;

using system.data;

using system.data.sqlclient;

using system.web.ui.htmlcontrols;

using system.drawing.imaging;

using system.configuration;

using system.drawing;

namespace zhuanti

/// <summary>

/// 这是一个用于玩家投稿中实现玩家上传文件功能中用到的相应的函数的功能模块

/// </summary>

public class fileupload

public enum file //定义一个人用于存放玩家上传文件信息的一个数组

file_size , //大小

file_postname, //类型(文件后缀名)

file_sysname , //系统名

file_orginname, //原来的名字

file_path //文件路径

private static random rnd = new random(); //获取一个随机数

public static string[] uploadfile(htmlinputfile file,string upload_dir) //实现玩家文件上传功能的主函数

string[] arr = new string[5];

string filename = getuniquelystring(); //获取一个不重复的文件名

string fileorginname = file.postedfile.filename.substring

(file.postedfile.filename.lastindexof("\")+1);//获取文件的原始名

if(file.postedfile.contentlength<=0)

{ return null; }

string postfilename;

string filepath = upload_dir.tostring();

string path = filepath + "\";

try

int pos = file.postedfile.filename.lastindexof(".")+1;

postfilename = file.postedfile.filename.substring(pos,file.postedfile.filename.length-pos);

file.postedfile.saveas(path+filename+"."+postfilename); //存储指定的文件到指定的目录

catch(exception exec)

throw(exec);

double unit = 1024;

double size = math.round(file.postedfile.contentlength/unit,2);

arr[(int)file.file_size] = size.tostring(); //文件大小

arr[(int)file.file_postname] = postfilename; //文件类型(文件后缀名)

arr[(int)file.file_sysname] = filename; //文件系统名

arr[(int)file.file_orginname] = fileorginname; //文件原来的名字

arr[(int)file.file_path]=path+filename+"."+postfilename; //文件路径

return arr;

public static bool operatedb(string sqlstr) //建立一个和数据库的关联

if (sqlstr==string.empty)

return false;

sqlconnection myconnection = new sqlconnection(configurationsettings.appsettings["connstring"]);

sqlcommand mycommand = new sqlcommand(sqlstr, myconnection);

myconnection.open();

mycommand.executenonquery();

myconnection.close();

return true;

public static string getuniquelystring() //获取一个不重复的文件名

const int random_max_value = 1000;

string strtemp,stryear,strmonth,strday,strhour,strminute,strsecond,strmillisecond;

datetime dt =datetime.now;

int rndnumber = rnd.next(random_max_value);

stryear = dt.year.tostring ();

strmonth = (dt.month > 9)? dt.month.tostring() : "0" + dt.month.tostring();

strday = (dt.day > 9)? dt.day.tostring() : "0" + dt.day.tostring();

strhour = (dt.hour > 9)? dt.hour.tostring() : "0" + dt.hour.tostring();

strminute = (dt.minute > 9)? dt.minute.tostring() : "0" + dt.minute.tostring();

strsecond = (dt.second > 9)? dt.second.tostring() : "0" + dt.second.tostring();

strmillisecond = dt.millisecond.tostring();

strtemp = stryear + strmonth + strday +"_"+ strhour + strminute + strsecond +"_"+ strmillisecond +"_"+ rndnumber.tostring () ;

return strtemp;