标题:图片上传报错
只看楼主
zdloveday
Rank: 2
等 级:论坛游民
帖 子:221
专家分:96
注 册:2008-9-5
结帖率:68.89%
 问题点数:0 回复次数:0 
图片上传报错
程序代码:
页面代码:
<form id="form1" method="post" enctype="multipart/form-data" runat="server">

 <p>
     <span>图片:</span>
     <input type="file" name="file" accept="image/*" />

 </p>
</form>

js

 _createForm: function (iframe) {
            var settings = this._settings;

            // We can't use the following code in IE6
            // var form = document.createElement('form');
            // form.setAttribute('method', 'post');
            // form.setAttribute('enctype', 'multipart/form-data');
            // Because in this case file won't be attached to request                    
            var form = toElement('<form method="post" enctype="multipart/form-data"></form>');

            form.setAttribute('action', settings.action);
            form.setAttribute('target', iframe.name);
            form.style.display = 'none';
            document.body.appendChild(form);

            // Create hidden input element for each data key
            for (var prop in settings.data) {
                if (settings.data.hasOwnProperty(prop)) {
                    var el = document.createElement("input");
                    el.setAttribute('type', 'hidden');
                    el.setAttribute('name', prop);
                    el.setAttribute('value', settings.data[prop]);
                    form.appendChild(el);
                }
            }
            return form;
        },
后端代码:

 try
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    string fileName = DateTime.Now.Ticks + "." + file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
                    string dateFolder = @"Uploads\" + DateTime.Now.Year + @"\" + DateTime.Now.Month + @"\" + DateTime.Now.Day + @"\";
                    string savePath = AppDomain.CurrentDomain.BaseDirectory + dateFolder;
                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }
                    savePath += fileName;
                    file.SaveAs(savePath);
                    return "/" + dateFolder.Replace(@"\", "/") + fileName;
                }
            }
            catch (Exception exp)
            {
                LogHelper.WriteLog("UploadImg:" + exp.ToString(), LogType.Exception);
            }

错误提示:
Request.Files.Count = “Request.Files.Count”引发了类型“System.NullReferenceException”的异常    
搜索更多相关主题的帖子: form file var request DateTime 
2019-07-11 16:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-495797-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059909 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved