下面是用ASP.NET写的,控件用的是upload
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NEAA.Model;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
///
///UpLoad 的摘要说明
///
public class UpLoad
{
public UpLoad()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public clsFileUpLoad FileUpLoad(FileUpload InputFile, string savePath, Page p) //FileUpload InputFile为上传控件名称
{
clsFileUpLoad fp = new clsFileUpLoad();
string fileName, fileExtension; //上传时的文件名 扩展名
string saveName = Guid.NewGuid().ToString(); //文件在服务器中存储的名字
string saveurl = savePath;//存储路径
//建立上传对象
HttpPostedFile postedFile = InputFile.PostedFile;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
int fileSize = postedFile.ContentLength;
fileExtension = System.IO.Path.GetExtension(fileName);
//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(savePath);
if (!upDir.Exists)
{
upDir.Create();
}
//保存文件
try
{
//string str = savePath + fileName+'_'+saveName;
InputFile.SaveAs(savePath+saveName);
}
catch
{
throw;
}
finally
{
InputFile.FileContent.Close();
}
fp.Filesavename = saveName;
fp.Filefilename = fileName;
fp.Filefilesize = fileSize;
fp.FilefileExtension = fileExtension;
fp.FilefileSavepath = saveurl;
return fp;
}
}