无论如何,在页面加载后填充Page_Load范围内的空对象列表

is there anyway to populate empty list of objects within the scope of Page_Load after page loading?

本文关键字:范围内 Load 列表 对象 Page 填充 加载 无论如何      更新时间:2023-09-26

>问题是我想用用户使用 dropzone js 上传的一些文件名填充var docfiles = new List<string>();,但列表不会填充,因为第一次页面加载httpRequest.Files.Count > 0这意味着用户没有上传文件,有没有想法在用户上传文件后填写列表,并且肯定会在页面加载后

  protected void Page_Load(object sender, EventArgs e)
{

    var httpRequest = System.Web.HttpContext.Current.Request;
    HttpFileCollection uploadFiles = httpRequest.Files;
    var docfiles = new List<string>();

    if (httpRequest.Files.Count > 0)
    {
        int i;
        for (i = 0; i < uploadFiles.Count; i++)
        {
            HttpPostedFile postedFile = uploadFiles[i];
            int fileSizeInBytes = postedFile.ContentLength;
            string fileName = postedFile.FileName;// Request.Headers["X-File-Name"];
            string fileExtension = "";
            fileExtension = Path.GetExtension(fileName);
            string savedFileName = Guid.NewGuid().ToString() + fileExtension;
            string path = HttpContext.Current.Server.MapPath("~/img/items/");
            string filename = path + savedFileName;
            postedFile.SaveAs(filename);
            docfiles.Add(filename);
        }
        itm.img1 = "ASs";
    }
  }

IsPostBack 属性将告诉您这是初始页面加载还是回发的结果,因此请在检查上传的文件之前对其进行检查。

MSDN Docs on IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        //your code
    }
}