国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

asp.net利用ashx文件實現(xiàn)文件的上傳功能

2024-07-09 22:42:01
字體:
來源:轉載
供稿:網(wǎng)友

原來以為文件上傳是一個比較簡單的功能,結果搞了一個晚上才搞定~這里主要介紹兩種方法實現(xiàn)。

方法一:Form表單提交

html代碼:

<!DOCTYPE html><html><head>  <meta charset="utf-8" />  <title>上傳文件</title>  <script src="Scripts/jquery-1.11.3.min.js"></script></head><body>  <form action="UploadHandler.ashx" method="post" enctype="multipart/form-data">    <input  name="file_upload" type="file" />    <input  type="submit" value="上傳" />  </form></body></html>

UploadHandler.ashx代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication1{  /// <summary>  /// UploadHandler 的摘要說明  /// </summary>  public class UploadHandler : IHttpHandler  {    public void ProcessRequest(HttpContext context)    {      context.Response.ContentType = "text/plain";      HttpPostedFile file = context.Request.Files["file_upload"];      string filePath = context.Server.MapPath("~/UploadFiles/") + System.IO.Path.GetFileName(file.FileName);      file.SaveAs(filePath);      context.Response.Write("上傳文件成功");    }    public bool IsReusable    {      get      {        return false;      }    }  }}

該方法雖然能夠實現(xiàn)文件的上傳,但是form表單提交之后整個頁面就刷新了,如果要無刷新上傳文件的話,就要使用ajax了。

方法二:jquery + ajax無刷上傳

html代碼:

<!DOCTYPE html><html><head>  <meta charset="utf-8" />  <title>上傳文件</title>  <script src="Scripts/jquery-1.11.3.min.js"></script></head><body>  <input  name="file_upload" type="file" />  <input  type="button" value="上傳" />  <script>    $(document).ready(function ()    {      $('#btn_upload').bind('click', function ()      {        var formData = new FormData();        formData.append('upload_file', $('#file_upload')[0].files[0]);        $.ajax({          url: 'UploadHandler.ashx',          type: 'post',          data: formData,          contentType: false,          processData: false,          success: function (msg)          {            if (msg == "Yes")            {              alert('文件上傳成功');            }            else            {              alert('文件上傳失敗');            }          }        })      });    });  </script></body></html>

UploadHandler.ashx代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication1{  /// <summary>  /// UploadHandler 的摘要說明  /// </summary>  public class UploadHandler : IHttpHandler  {    public void ProcessRequest(HttpContext context)    {      context.Response.ContentType = "text/plain";      if (context.Request.Files.Count > 0)      {        HttpPostedFile file = context.Request.Files["upload_file"];        string filePath = context.Server.MapPath("~/UploadFiles/") + System.IO.Path.GetFileName(file.FileName);        file.SaveAs(filePath);        context.Response.Write("Yes");      }      else      {        context.Response.Write("No");      }    }    public bool IsReusable    {      get      {        return false;      }    }  }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 翼城县| 兴义市| 陵川县| 邛崃市| 香港| 济宁市| 巴青县| 昭觉县| 偃师市| 盐山县| 寿光市| 遵化市| 平乡县| 晋江市| 芦溪县| 长葛市| 莒南县| 龙门县| 东安县| 武功县| 岳阳县| 广灵县| 洪泽县| 都安| 班戈县| 晋中市| 祁连县| 东港市| 苍溪县| 大名县| 郎溪县| 新昌县| 上蔡县| 苍山县| 康平县| 阿拉善右旗| 额济纳旗| 寿阳县| 黄山市| 财经| 宁阳县|