注册 登录
编程论坛 ASP.NET技术论坛

图片上传报错

zdloveday 发布于 2019-07-11 16:06, 2637 次点击
程序代码:
页面代码:
<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”的异常   
0 回复
1