国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 遵化市| 五原县| 遂昌县| 周至县| 公主岭市| 黑河市| 当涂县| 阿拉善右旗| 黄陵县| 西丰县| 泸州市| 屯昌县| 庆城县| 华池县| 定边县| 麻江县| 昌乐县| 抚松县| 南川市| 筠连县| 乌拉特前旗| 盐山县| 县级市| 新津县| 军事| 南澳县| 大厂| 迁西县| 大港区| 杭锦后旗| 徐闻县| 玉林市| 丹阳市| 满城县| 乃东县| 德州市| 江口县| 改则县| 辽宁省| 阳东县| 漾濞|