【问题标题】:why the list<> is not populating in this code?为什么 list<> 没有填充在这段代码中?
【发布时间】:2016-06-11 19:07:51
【问题描述】:

当客户端使用 dropzone js 发布文件时,我正在尝试在字符串列表 List&lt;string&gt; img = new List&lt;string&gt;(); 中填充文件路径 上传的文件没有例外,但列表不会填充,有解决这个问题的想法吗?

protected void Page_Load(object sender, EventArgs e)
{
    var httpRequest = System.Web.HttpContext.Current.Request;
    HttpFileCollection uploadFiles = httpRequest.Files;
    List<string> img = new List<string>();

    if (IsPostBack)
    {
        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);
                img.Add(filename);
            }
            itm.img1 = img[0];
        }
    }

【问题讨论】:

  • 运行代码后img.Count的值是多少?你怎么知道它不会填充?你有例外吗?需要更多信息。
  • @LukePark 好的,我会编辑我的问题
  • @LukePark img.Count 运行此代码后为 0
  • 什么是 httpRequest.Files.Count ?
  • @ZivWeissman 当页面加载时httpRequest.Files.Count 为 0,但在用户发布文件后,它会增加与发布文件一样多

标签: c# asp.net ajax dropzone.js


【解决方案1】:

我从

运行他们的项目

https://github.com/venkatbaggu/dropzonefileupload

页面中的 js(根据他们的演示)是

//来自服务器的文件上传响应 Dropzone.options.dropzoneForm = { 最大文件数:2, url: "WebFormDropzoneDemo.aspx", 初始化:函数(){ this.on("maxfilesexceeded", function (data) { var res = eval('(' + data.xhr.responseText + ')'); }); this.on("addfile", function (file) { // 创建删除按钮 var removeButton = Dropzone.createElement("删除文件"); // 将 Dropzone 实例捕获为闭包。 var _this = 这个; // 监听点击事件 removeButton.addEventListener("点击", function (e) { // 确保按钮点击不会提交表单: e.preventDefault(); e.stopPropagation(); // 删除文件预览。 _this.removeFile(文件); // 如果你也想删除服务器上的文件, // 你可以在这里做 AJAX 请求。 }); // 将按钮添加到文件预览元素。 file.previewElement.appendChild(removeButton); }); } };

这可以很好地填充 Request.Files。

公共部分类 WebFormDropzoneDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { 如果(!IsPostBack) { SaveUploadedFile(Request.Files); } } 公共无效SaveUploadedFile(HttpFileCollection httpFileCollection) { bool isSavedSuccessfully = true; 字符串 fName = ""; foreach(httpFileCollection 中的字符串文件名) { HttpPostedFile 文件 = httpFileCollection.Get(fileName); //保存文件内容到这里 fName = 文件.文件名; if (file != null && file.ContentLength > 0) { var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\WallImages", Server.MapPath(@"\"))); string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "imagepath"); var fileName1 = Path.GetFileName(file.FileName); bool isExists = System.IO.Directory.Exists(pathString); 如果 (!isExists) System.IO.Directory.CreateDirectory(pathString); var path = string.Format("{0}\\{1}", pathString, file.FileName); 文件.另存为(路径); } } //if (isSavedSuccessfully) //{ // 返回 Json(new { Message = fName }); //} //别的 //{ // return Json(new { Message = "保存文件时出错" }); //} } }

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2016-05-16
    • 2017-02-21
    相关资源
    最近更新 更多