使用ajax上傳文件

    1. 首先針對aspx頁面
    2. <form id= "uploadForm">  
    3.       <>指定文件名: <input type="text" name="filename" value= ""/></>  
    4.       <>上传文件: <input type="file" name="file"/></ p>  
    5.       <input type="button" value="上传" onclick="doUpload()" />  
    6. </form

      ajax如下

  1. function doUpload() {  
  2.      var fd= new FormData($( "#uploadForm" )[0]);  //uploadForm為from表單id
  3.      $.ajax({  
  4.           url: 'XXXXXX' ,  
  5.           type: 'post',  
  6.           data: fd,  
  7.           async: false,  
  8.           cache: false,  
  9.           processData: false,  //  //這兩行代碼一定要加上  (默认: true) 默认情况下,发送到服务器的数据(即data参数)将被转换为字符串以配合默认内容类型                    
  10.                                        //  "application/x-www-form-urlencoded"。
                                           //如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。
  11.           contentType: false, //默認為string類型   "application/x-www-form-urlencoded"
  12.           success: function (returndata) {  
  13.               alert(returndata);  
  14.           },  
  15.           error: function (returndata) {  
  16.               alert(returndata);  
  17.           }  
  18.      });  

 

例子: 

<form id="form1" runat="server">
        <div>
            &nbsp;<br />
            <input id="fup_two" name="fup_two" type="file" />

            <input id="tryone" type="button" value="嘗試第二個" onclick="a()" />
        </div>

</form>

 

//注意低版本的jquery不支持.

    <script>
        function a() {
            var file = $("#fup_two")[0].files[0];
            if (file.size > 1200000000) {
                alert("文件過大,不能發送.");
                return;
            }
            else {
                //buzhichi jquery 1.4......
                var fd = new FormData($("#form1")[0]);
                $.ajax({
                    type: "post",
                    url: "ashx/ajax.ashx?type=up",
                    data: fd,
                    dataType: "text",
                    async: false,
                    cache: false,
                    processData: false,//must 加上(默认: true) 默认情况下,发送到服务器的数据(即data参数)将被转换为字符串以配合默认内容类型 "application/x-www-form-urlencoded"。
                    //如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。
                    contentType: false,//must 加上processData contentType必須配合使用
                    success: function (data) {
                        alert(1);
                        alert(data);
                    },
                    error: function (data) {
                        alert(2);
                    }
                });
            }
        }
    </script>

 

在一般處理程序中,關鍵代碼如下.

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string types = context.Request.Params["type"];

            switch (types)
            {
                case "ajax":
                    Getstr();
                    break;
                case "up":
                    up(context);
                    break;
                default:
                    break;
            }
        }

        public void Getstr()
        {
            HttpContext.Current.Response.Write("asaf");
        }
        public void up(HttpContext context)
        {
            HttpPostedFile hpf = context.Request.Files["fup_two"];
            string strs = HttpContext.Current.Server.MapPath("Temp");
            string filename=DateTime.Now.ToLongDateString()+Path.GetExtension(hpf.FileName);
            hpf.SaveAs(strs+"/"+filename);
            if (File.Exists(strs+"/"+ filename))
            {
                context.Response.Write("上傳成功!");
            }
            else
            {
                context.Response.Write("上傳失敗!");
            }
        }

 

posted @ 2016-03-28 16:01  好好學習  阅读(46)  评论(0编辑  收藏  举报