asp教程.net+jquery 文件上传代码
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
上传文件 方法
///
///
///
public string uploadfile(string filenamepath)
{
//system.threading.thread.sleep(10000);
//system.net.mime.mediatypenames.application.doevents();
return uploadfile(filenamepath, "upload/orgimg/");
}
///
/// 上传文件 方法
///
///
///
///
public string uploadfile(string filenamepath, string tofilepath)
{
try
{
//获取要保存的文件信息
fileinfo file = new fileinfo(filenamepath);
//获得文件扩展名
string filenameext = file.extension;
//验证合法的文件
if (checkfileext(filenameext))
{
//生成将要保存的随机文件名
string filename = getfilename() + filenameext;
//检查保存的路径 是否有/结尾
if (tofilepath.endswith("/") == false) tofilepath = tofilepath + "/";
//if (smallpath.endswith("/") == false) tofilepath = tofilepath + "/";
//if (waterpath.endswith("/") == false) tofilepath = tofilepath + "/";
//按日期归类保存
string datepath = datetime.now.tostring("yyyymm") + "/" + datetime.now.tostring("dd") + "/";
if (true)
{
tofilepath += datepath;//原图
// smallpath += datepath;//缩略图
// waterpath += datepath;//水印图
}
//获得要保存的文件路径 --upload/orgimg/2010/07/02/asdafsaf.jpg
string serverfilename = tofilepath + filename;
//获得要保存的缩略图文件路径
// string smallfilename = smallpath + filename;
//获得要保存的水印图文件路径
// string waterfilename = waterpath + filename;
//物理完整路径 --服务器上路径
string tofilefullpath = httpcontext.current.server.mappath(tofilepath);
//缩略图物理完整路径
// string tosmallfullpath = httpcontext.current.server.mappath(smallpath);
//水印图物理完整路径
// string toswaterfullpath = httpcontext.current.server.mappath(waterpath);
//检查是否有该路径 没有就创建
if (!directory.exists(tofilefullpath))
{
directory.createdirectory(tofilefullpath);
}
//检查缩略图是否有该路径 没有就创建
//if (!directory.exists(tosmallfullpath))
//{
// directory.createdirectory(tosmallfullpath);
//}
////检查水印图是否有该路径 没有就创建
//if (!directory.exists(toswaterfullpath))
//{
// directory.createdirectory(toswaterfullpath);
//}
//将要保存的完整文件名 --绝对路劲
string tofile = tofilefullpath + filename;
//将要保存的缩略图完整文件名
//string tosmallfile = tosmallfullpath + filename;
////将要保存的水印图完整文件名
//string towaterfile = toswaterfullpath + filename;
///创建webclient实例
webclient mywebclient = new webclient();
//设定windows网络安全认证 方法1
mywebclient.credentials = credentialcache.defaultcredentials;
////设定windows网络安全认证 方法2
//networkcredential cred = new networkcredential("username", "userpwd");
//credentialcache cache = new credentialcache();
//cache.add(new uri("uploadpath"), "basic", cred);
//mywebclient.credentials = cache;
//httpfilecollection files = httpcontext.current.request.files;
//for (int ifile = 0; ifile < files.count; ifile++)
//{
// httppostedfile postedfile = files[ifile];
// postedfile.saveas(tofile);
//}
// 可根据扩展名字的不同保存到不同的文件夹
//要上传的文件
filestream fs = new filestream(filenamepath, filemode.open, fileaccess.read);
//filestream fs = openfile();
binaryreader r = new binaryreader(fs);
//使用uploadfile方法可以用下面的格式
//mywebclient.uploadfile(tofile, "put",filenamepath);
byte[] postarray = r.readbytes((int)fs.length);
stream poststream = mywebclient.openwrite(tofile, "put");
if (poststream.canwrite)
{
poststream.write(postarray, 0, postarray.length);
}
else
{
return "0|" + serverfilename + "|" + "文件目前不可写";
}
poststream.close();
// makethumbnail(tofile, tosmallfile, 80, 80, "cut");
// addwater(tofile, towaterfile);
return "1|" + serverfilename + "|" + "文件上传成功|" + serverfilename;
}
else
{
return "0|errorfile|" + "文件格式非法";
}
}
catch (exception e)
{
return "0|errorfile|" + "文件上传失败,错误原因:" + e.message;
}
}