获取配置文件httpRuntime+XMLHttpRequest+一般处理程序

配置文件:

<system.web>
      <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false" />

</system.web>

后台:

if (!IsPostBack)
{
    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    object o = config.GetSection("system.web/httpRuntime");
    HttpRuntimeSection section = o as HttpRuntimeSection;
    nhid_MaxRequestLength.Value = section.MaxRequestLength.ToString();
}

前台:

function UpladFile() {
    //debugger;
    var arr = "";
    var files = document.getElementById("descFile").files;
    var fd = new FormData();
    var sumSize = 0;
    for (var i = 0; i < files.length; i++) {
         var file = files[i];
         sumSize += file.size / 1024;
    var fileName = file.name;
    fd.append(‘myfile‘ + i, file);

  if (arr.indexOf(fileName) == -1) {
            arr += fileName + ";";
        }
     }

 if (sumSize > parseInt($get(‘nhid_MaxRequestLength‘).value)) {
         alert(‘一次上传文件大小的总和不能超过‘ + $get(‘nhid_MaxRequestLength‘).value + ‘KB!‘);
    return;
 }

     var FileController = "/Service/UploadifyFile.ashx?billType=Task&comType=add&keyValue=" + $get(‘ihid_TaskRecM_Id‘).value;
     // XMLHttpRequest 对象
     var xhr = new XMLHttpRequest();
     //初始化XHLHttpRequest
     xhr.open("post", encodeURI(FileController), true);
     //发送请求
  xhr.send(fd);
  //回调函数
  xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    SaveDTable2(arr);
  }
 }
}

<asp:HiddenField runat="server" ID="nhid_MaxRequestLength" />

一般处理程序:

public class UploadifyFile : IHttpHandler
{
  string AffixPath = System.Configuration.ConfigurationSettings.AppSettings["AffixPath"].ToString();
public void ProcessRequest(HttpContext context)
{
  context.Response.ContentType = "text/plain";
  //context.Response.Write("Hello World");

  string comType = context.Request["comType"].ToString();
  switch (comType)
  {
    case "add":
      UpLoad(context, 1);
      break;
    case "add2":
      UpLoad(context, 2);
      break;
    case "del":
      Del(context);
      break;
    default:
    break;
  }


}

/// <summary>
/// 上传
/// </summary>
/// <param name="context"></param>
/// <param name="type">1:任务、项目;2:资料</param>
public void UpLoad(HttpContext context, int type)
{
  string strFileFullName = "", strFileName = "", strSaveFileName = "", strSaveFilePath = "", conStr = "";
  string billType = context.Request["billType"].ToString();
  string keyValue = context.Request["keyValue"].ToString();

  switch (type)
  {
    case 1:
      strSaveFilePath = AffixPath + "/temp/" + billType + "/" + keyValue + "/";
      break;
    case 2:
      strSaveFilePath = AffixPath + "/" + keyValue + "/";
      break;
  }

  if (!Directory.Exists(strSaveFilePath))
  {
    Directory.CreateDirectory(strSaveFilePath);
  }


  HttpFileCollection files = context.Request.Files;
  for (int i = 0; i < files.Count; i++)
  {
    if (files[i].ContentLength > 0)
    {
      strFileFullName = files[i].FileName;
      int index = strFileFullName.LastIndexOf(@"\");
      strFileName = (index < 0) ? strFileFullName : strFileFullName.Substring(index + 1);

      strSaveFileName = strSaveFilePath + strFileName;

      if (System.IO.File.Exists(strSaveFileName))
      {
        File.Delete(strSaveFileName);
      }
      if (!string.IsNullOrEmpty(strFileName))
      {
        files[i].SaveAs(strSaveFileName);
      }
    }
  }
}

public void Del(HttpContext context)
{
  string billType = context.Request["billType"].ToString();
  string keyValue = context.Request["keyValue"].ToString();
  string file = context.Request["file"].ToString();
  string strSaveFilePath = AffixPath + "/temp/" + billType + "/" + keyValue + "/" + file;
  if (System.IO.File.Exists(strSaveFilePath))
  {
    File.Delete(strSaveFilePath);
  }
}

public bool IsReusable
{
  get
  {
    return false;
  }
}

获取配置文件httpRuntime+XMLHttpRequest+一般处理程序

上一篇:Serverless Web Function 实践教程(二):基于 Web 函数部署您的 Flask 项目


下一篇:《大数据导论》——2.6节案例学习