多个文件上传asp.net并在数据库中保存文件名

时间:2017-12-03 18:00:07

标签: asp.net

我有一个jquery多文件上传器 它使用ajax代码使用File处理程序上传文件 这是上传代码:

.exe

这是Filehander.ashx中的c#sharp代码

  var selectedFiles;

  $(document).ready(function () {

    if (!Modernizr.draganddrop) {
      alert("This browser doesn't support File API and Drag & Drop features of HTML5!");
      return;
    }

    var box;
    box = document.getElementById("box");
    box.addEventListener("dragenter", OnDragEnter, false);
    box.addEventListener("dragover", OnDragOver, false);
    box.addEventListener("drop", OnDrop, false);

    $("#upload").click(function () {
      var data = new FormData();
      for (var i = 0; i < selectedFiles.length; i++) {
        data.append(selectedFiles[i].name, selectedFiles[i]);
      }
      $.ajax({
        type: "POST",
        url: "FileHandler.ashx",
        contentType: false,
        processData: false,
        data: data,
        success: function (result) {
          alert(result);
        },
        error: function () {
          alert("There was error uploading files!");
        }
      });
    });

  });

  function OnDragEnter(e) {
    e.stopPropagation();
    e.preventDefault();
  }

  function OnDragOver(e) {
    e.stopPropagation();
    e.preventDefault();
  }

  function OnDrop(e) {
    e.stopPropagation();
    e.preventDefault();
    selectedFiles = e.dataTransfer.files;
    $("#box").text(selectedFiles.length + " file(s) selected for uploading!");
  }


</script>

但是当我点击Asp.net按钮时,我想在我的主页面上显示这些文件名 如何获取文件名?

1 个答案:

答案 0 :(得分:0)

您可以使用如下的数据类;

public class FileDataModel
{
    public Guid Id{ get; set; }
    public string FileName { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateDeleted { get; set; }
}

你的处理程序如下,也许你可以检查其他空状态或文件存在等;

public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                using (FileDbContext ctx = new FileDbContext())
                {
                    foreach (string key in files)
                    {
                        try
                        {
                            HttpPostedFile file = files[key];
                            string fileName = file.FileName;
                            fileName = context.Server.MapPath("~/uploads/" + fileName);
                            file.SaveAs(fileName);
                            ctx.Files.Add(new FileDataModel()
                            {
                                DateCreated = DateTime.Now,
                                FileName = file.FileName,
                                Id = Guid.NewGuid()
                            })
                        }
                        catch (Exception e)
                        {
                        }

                    }
                    ctx.SaveChanges();
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("File(s) uploaded successfully!");
        }