asp.net – 如何将ContextKeys属性用于AjaxFileUpload控件?
我开始查看AjaxFileUpload控件,特别是ContextKeys属性.但是,我不明白如何使用它.
The documentation对AjaxFileUpload说,ContextKeys用于在上传文件时将信息传递给服务器.但没有提供任何例子.我在网上有任何可以看的例子吗? 解决方法
虽然没有实现这样的功能(我相信它是有计划的,但由于某些原因被推迟),但没有什么可以保护你自己实现它.为此,您需要下载AjaxControlToolkit源代码并根据您的需要进行调整.
会有很多积分,所以你可以准备一杯咖啡:) 我将显示必须更改的文件名的更改. Server / AjaxControlToolkit / AjaxFileUpload / AjaxFileUpload.cs文件 首先,将ContextKeys属性添加到AjaxFileUploadEventArgs.cs文件(它位于同一文件夹中): /// <summary> /// Gets or sets the context keys. /// </summary> public string ContextKeys { get; set; } 之后打开AjaxFileUpload类代码并更改OnPreRender方法.以下是此方法的一部分,包含自定义修改: var eventArgs = new AjaxFileUploadEventArgs(guid,AjaxFileUploadState.Success,"Success",uploadedFile.FileName,uploadedFile.ContentLength,uploadedFile.ContentType,stream.ToArray()); // NEW CODE HERE eventArgs.ContextKeys = this.Page.Request.Form["contextKeys"]; 这就是我们需要的服务器代码的所有变化.现在我们需要修改Sys.Extended.UI.AjaxFileUpload客户端类(文件AjaxFileUpload.pre.js) 首先让我们修改_html5UploadFile方法如下: _html5UploadFile: function (fileItem) { this._guid = Sys.Extended.UI.AjaxFileUpload.utils.generateGuid(); var uploadableFile = fileItem.get_fileInputElement(); var fd = new FormData(); fd.append("fileId",uploadableFile.id); fd.append("Filedata",uploadableFile.file); if (this.contextKeys) { if (typeof this.contextKeys !== "string") { this.contextKeys = Sys.Serialization.JavaScriptSerializer.serialize(this.contextKeys); } fd.append("contextKeys",this.contextKeys); } $common.setVisible(this._progressBar,true); this._setDisableControls(true); this._html5SetPercent(0); this._setStatusMessage(String.format(Sys.Extended.UI.Resources.AjaxFileUpload_UploadingHtml5File,uploadableFile.file.name,Sys.Extended.UI.AjaxFileUpload.utils.sizeToString(uploadableFile.file.size))); var url = this._postBackUrl; if (url.indexOf("?") != -1) url += "&"; else url += "?"; this._webRequest = new Sys.Net.WebRequest(); this._executor = new Sys.Net.XMLHttpExecutor(); this._webRequest.set_url(url + 'contextkey=' + this._contextKey + '&guid=' + this._guid); this._webRequest.set_httpVerb("POST"); this._webRequest.add_completed(this.bind(this._html5OnRequestCompleted,this)); //this._executor.add_load(this.bind(this._html5OnComplete,this)); this._executor.add_progress(this.bind(this._html5OnProgress,this)); this._executor.add_uploadAbort(this.bind(this._html5OnAbort,this)); this._executor.add_error(this.bind(this._html5OnError,this)); this._webRequest.set_executor(this._executor); this._executor.executeRequest(fd); } 如您所见,我们将contextKeys添加到表单数据中,并使用Ajax请求发布. 我们需要修改_uploadInputElement方法: _uploadInputElement: function (fileItem) { var inputElement = fileItem.get_fileInputElement(); var uploader = this; uploader._guid = Sys.Extended.UI.AjaxFileUpload.utils.generateGuid(); setTimeout(function () { uploader._setStatusMessage(String.format(Sys.Extended.UI.Resources.AjaxFileUpload_UploadingInputFile,Sys.Extended.UI.AjaxFileUpload.utils.getFileName(inputElement.value))); uploader._setDisableControls(true); uploader.setThrobber(true); },0); var url = uploader._postBackUrl; if (url.indexOf("?") != -1) url += "&"; else url += "?"; uploader._createVForm(); uploader._vForm.appendChild(inputElement); if (this.contextKeys) { if (typeof this.contextKeys !== "string") { this.contextKeys = Sys.Serialization.JavaScriptSerializer.serialize(this.contextKeys); } var contextKeysInput = document.createElement("input"); contextKeysInput.setAttribute("type","hidden"); contextKeysInput.setAttribute("name","contextKeys"); contextKeysInput.setAttribute("value",this.contextKeys); uploader._vForm.appendChild(contextKeysInput); } uploader._vForm.action = url + 'contextkey=' + this._contextKey + '&guid=' + this._guid; uploader._vForm.target = uploader._iframeName; setTimeout(function () { uploader._vForm.submit(); uploader._waitTimer = setTimeout(function () { uploader._wait() },100); },0); } 完成所有这些更改后,您可以在代码隐藏中设置ContextKeys属性,并从UploadComplete事件的AjaxFileUploadEventArgs参数中获取它,如下所示: protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack && !AjaxFileUpload1.IsInFileUploadPostBack) { AjaxFileUpload1.ContextKeys = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new Dictionary<string,string> { { "1","First" },{ "2","Second" } }); } protected void AjaxFileUpload1_OnUploadComplete(object sender,AjaxFileUploadEventArgs file) { if (!string.IsNullOrEmpty(file.ContextKeys)) { var contextKeys = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string,string>>(file.ContextKeys); } 此外,如果您将实现此处建议的OnClientUploadStarted客户端事件link,您可以从客户端传递给服务器您的contextKeys: function uploadStarted(sender,args) { sender.contextKeys = { "first": "1","second": "2" }; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – ASP.NET MVC 3一对多无法更新桥表
- asp.net-mvc – JQuery中的ViewBag
- asp.net-mvc-3 – ASP.NET MVC中全局错误/异常处理的最佳实
- asp.net – CheckBoxList滚动条
- asp.net-mvc – ASP.NET MVC 5自定义错误页面
- .Net Core技术研究-Span<T>和ValueTuple<T&
- asp.net – 保持visual studio从bin / on重建中删除所有内容
- ASP.NET MVC5 中百度ueditor富文本编辑器的使用(转)
- asp.net – dbo.aspnet_Users和dbo.aspnetUsers之间有什么区
- asp.net – jQuery模态窗体对话框回发问题
- asp.net – 为什么当使用ADO.NET实体数据模型映射
- 寻找一个使用Lucene.net与ASP.NET的例子
- asp.net – 将数据发送到存储过程
- asp.net-mvc – Olark免费替代品
- 使用ASP.NET MVC4 jquery/javascript捆绑
- asp.net-mvc – 如何在Visual Studio 2015中快速
- asp.net-mvc-4 – 尝试在displaytemplate中访问父
- C#_.net core 3.0自定义读取.csv文件数据_解决首
- asp.net – 在web-farm网站上将machineKey添加到
- ASP.NET Razor大型项目的网页?