asp教程.net+jquery 檔案上傳代碼
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script language="網頁特效">
unction testup()
{
ajaxfileupload("fileupload1");
}
function ajaxfileupload(obfile_id)
{
//準備提交處理 $("#loading_msg").html("<img src="/images/dotajax.gif" />");
//開始提交 $.ajax({
type: "post", url:"ajaxupfile.ashx", data:"upfile="+$("#"+obfile_id).val(),
success:function
(data, status)
{ //alert(data);
var stringarray = data.split("|"); if(stringarray[0]=="1")
{
//stringarray[0] 成功狀態(1為成功,0為失敗) //stringarray[1] 上傳成功的檔案名稱 //stringarray[2] 訊息提示
$("#divmsg").html("<img src="/images/note_ok.gif" />"+stringarray[2]+" 檔案地址:"+stringarray[1]);
$("#filepreview").attr({ src:stringarray[1]}); } else { //上傳出錯 $("#divmsg").html("<img
src="/images/note_error.gif" />"+stringarray[2]+""); } $("#loading_msg").html("");
},
error:function (data, status, e)
{ alert("上傳失敗:"+e.tostring()); } });
return false;//.net按鈕控制項取消提交}
</script>
</head>
<body>
asp.net教程 c代碼
上傳檔案 方法
/// </summary>
/// <param name="filenamepath"></param>
/// <returns></returns>
public string uploadfile(string filenamepath)
{
//system.threading.thread.sleep(10000);
//system.net.mime.mediatypenames.application.doevents();
return uploadfile(filenamepath, "upload/orgimg/");
}
/// <summary>
/// 上傳檔案 方法
/// </summary>
/// <param name="filenamepath"></param>
/// <param name="tofilepath"></param>
/// <returns>返回上傳處理結果 格式說明: 0|file.jpg|msg 成功狀態|檔案名稱|訊息 </returns>
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);
//}