asp.net+jquery 檔案上傳代碼(1/2)

來源:互聯網
上載者:User

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);
                //}

首頁 1 2 末頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.