濮阳杆衣贸易有限公司

主頁 > 知識庫 > 表單上傳功能實(shí)現(xiàn) ajax文件異步上傳

表單上傳功能實(shí)現(xiàn) ajax文件異步上傳

熱門標(biāo)簽:立陶宛地圖標(biāo)注 大眾點(diǎn)評400電話怎么申請 電銷機(jī)器人 長春 河間市地圖標(biāo)注app 東平縣地圖標(biāo)注app 中國地圖標(biāo)注不明確情況介紹表 地圖標(biāo)注推銷坑人 怎樣在地圖標(biāo)注文字 上海企業(yè)外呼系統(tǒng)價(jià)錢

項(xiàng)目中用戶上傳總是少不了的,下面就主要的列舉一下表單上傳和ajax上傳!注意: context.Request.Files不適合對大文件進(jìn)行操作,下面列舉的主要對于小文件上傳的處理!

資源下載:

一、jQuery官方下載地址:https://jquery.com/download/ 

一.表單上傳:

html客戶端部分:

form action="upload.ashx" method="post" enctype="multipart/form-data">
    選擇文件:input type="file" name="file1" />br />
    input type="submit" value="上傳" />
  /form>

一般處理程序服務(wù)器端:

public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      HttpPostedFile file1 = context.Request.Files["file1"];
      helper.uploadFile(file1, "~/upload/");//這里就是對相應(yīng)方法進(jìn)行調(diào)用
      context.Response.Write("ok");//提示執(zhí)行成功
    }

上傳代碼的封裝:

/// summary>
    /// 上傳圖片
    /// /summary>
    /// param name="file">通過form表達(dá)提交的文件/param>
    /// param name="virpath">文件要保存的虛擬路徑/param>
    public static void uploadImg(HttpPostedFile file,string virpath)
    {     
      if (file.ContentLength > 1024 * 1024 * 4)
      {
        throw new Exception("文件不能大于4M");
      }
      string imgtype = Path.GetExtension(file.FileName);
      if(imgtype!=".jpg"imgtype!=".jpeg") //圖片類型進(jìn)行限制
      {
        throw new Exception("請上傳jpg或JPEG圖片");
      }
      using (Image img = Bitmap.FromStream(file.InputStream))
      {
        string savepath = HttpContext.Current.Server.MapPath(virpath+file.FileName);
        img.Save(savepath);
      }
    }
    /// summary>
    /// 上傳文件
    /// /summary>
    /// param name="file">通過form表達(dá)提交的文件/param>
    /// param name="virpath">文件要保存的虛擬路徑/param>
    public static void uploadFile(HttpPostedFile file, string virpath)
    {
      if (file.ContentLength > 1024 * 1024 * 6)
      {
        throw new Exception("文件不能大于6M");
      }
      string imgtype = Path.GetExtension(file.FileName);
      //imgtype對上傳的文件進(jìn)行限制
      if (imgtype != ".zip"  imgtype != ".mp3")
      {
        throw new Exception("只允許上傳zip、rar....文件");
      }
      string dirFullPath= HttpContext.Current.Server.MapPath(virpath);
      if (!Directory.Exists(dirFullPath))//如果文件夾不存在,則先創(chuàng)建文件夾
      {
        Directory.CreateDirectory(dirFullPath);
      }
      file.SaveAs(dirFullPath + file.FileName);
    }

二.Ajax文件異步上傳:

注明:既然有了表單上傳為什么又要ajax上傳呢?因?yàn)楸韱紊蟼鬟^程中,整個(gè)頁面就刷新了!ajax異步上傳就可以達(dá)到只刷新局部位置,下面就簡單看看ajax上傳吧!

html客戶端部分:

head> 
script src="jquery-2.1.4.js">/script>
 script>
  $(function () {
   $("#upload").click(function () {
    $("#imgWait").show();
    var formData = new FormData();
    formData.append("myfile", document.getElementById("file1").files[0]); 
    $.ajax({
     url: "upload.ashx",
     type: "POST",
     data: formData,
     /**
     *必須false才會自動加上正確的Content-Type
     */
     contentType: false,
     /**
     * 必須false才會避開jQuery對 formdata 的默認(rèn)處理
     * XMLHttpRequest會對 formdata 進(jìn)行正確的處理
     */
     processData: false,
     success: function (data) {
      if (data.status == "true") {
       alert("上傳成功!");
      }
      if (data.status == "error") {
       alert(data.msg);
      }
      $("#imgWait").hide();
     },
     error: function () {
      alert("上傳失敗!");
      $("#imgWait").hide();
     }
    });
   });
  });
 /script>
/head>
body> 
  選擇文件:input type="file" id="file1" />br />
  input type="button" id="upload" value="上傳" />
  img src="wait.gif" style="display:none" id="imgWait" /> 
/body>


一般處理程序服務(wù)器端:

public void ProcessRequest(HttpContext context)
  {
   context.Response.ContentType = "text/html";
   if (context.Request.Files.Count > 0)
   {
    HttpPostedFile file1 = context.Request.Files["myfile"];
    helper.uploadFile(file1, "~/upload/"); //這里引用的是上面封裝的方法
    WriteJson(context.Response, "true", "");
   }
   else
   {
    WriteJson(context.Response, "error", "請選擇要上傳的文件");
   }
  }

json代碼封裝:

public static void WriteJson(HttpResponse response,
      string status1, string msg1, object data1 = null)
    {
      response.ContentType = "application/json";
      var obj = new { status = status1, msg = msg1, data = data1 };
      string json = new JavaScriptSerializer().Serialize(obj);
      response.Write(json);
    }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • ajax實(shí)現(xiàn)異步文件或圖片上傳功能
  • ajax圖片上傳,圖片異步上傳,更新實(shí)例
  • ajax 異步上傳帶進(jìn)度條視頻并提取縮略圖
  • Ajax異步文件上傳與NodeJS express服務(wù)端處理
  • Ajax異步上傳文件實(shí)例代碼分享
  • ajax實(shí)現(xiàn)文件異步上傳并回顯文件相關(guān)信息功能示例
  • jquery中的ajax異步上傳
  • Ajax表單異步上傳文件實(shí)例代碼(包括文件域)
  • ajax異步實(shí)現(xiàn)文件分片上傳實(shí)例代碼

標(biāo)簽:營口 玉樹 遼寧 益陽 內(nèi)江 四川 銅川 本溪

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《表單上傳功能實(shí)現(xiàn) ajax文件異步上傳》,本文關(guān)鍵詞  表單,上傳,功能,實(shí)現(xiàn),ajax,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《表單上傳功能實(shí)現(xiàn) ajax文件異步上傳》相關(guān)的同類信息!
  • 本頁收集關(guān)于表單上傳功能實(shí)現(xiàn) ajax文件異步上傳的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    吴堡县| 临颍县| 泗洪县| 安义县| 石台县| 石门县| 汉寿县| 隆安县| 吉林省| 鄯善县| 洛宁县| 农安县| 黑龙江省| 许昌县| 来凤县| 吴堡县| 谷城县| 景德镇市| 黄大仙区| 遂川县| 叶城县| 绥芬河市| 义马市| 绩溪县| 延寿县| 威宁| 宽甸| 轮台县| 松潘县| 湟源县| 苍山县| 胶南市| 木里| 双牌县| 武川县| 牟定县| 绍兴市| 南宫市| 滕州市| 荆门市| 新野县|