问题描述
我开始寻找在AjaxFileUpload控制,特别是ContextKeys财产。但是,我不知道如何使用它。
I started looking at the AjaxFileUpload control, specifically the ContextKeys property. However, I do not understand how to use it.
AjaxFileUpload的说,ContextKeys时使用的文件上传到信息传递到服务器的文档。但没有提供的例子。是否有任何在线的例子,我可以看看?
The documentation says of AjaxFileUpload that the ContextKeys is used to pass information to the server when a file is uploaded. But no examples are provided. Are there any examples online that I could look at?
推荐答案
虽然这样的功能没有实现(我相信这是计划,但因为某种原因被推迟),没有保护你自己实现它。要做到这一点,你需要下载AjaxControlToolkit源$ C $ c和调整它为您的需求。
Though such functionality not implemented (I believe it was planned but by some reasons was postponed), nothing protect you from implement it yourself. To do this you need to download AjaxControlToolkit source code and tweak it for your needs.
有会一分不少,所以你可以以prepare的前咖啡一杯:)
There will be a lot of points so you may to prepare a cup of coffee before :)
我将展示与必须被改变文件的名称更改。
I'll show changes with name of file that must being changed.
服务器/ AjaxControlToolkit / AjaxFileUpload / AjaxFileUpload.cs 文件
首先,ContextKeys属性添加到AjaxFileUploadEventArgs.cs文件(它位于同一文件夹中):
First of all, add ContextKeys property to the AjaxFileUploadEventArgs.cs file (it located in same folder):
/// <summary>
/// Gets or sets the context keys.
/// </summary>
public string ContextKeys
{
get;
set;
}
这是打开AjaxFileUpload类code和修改在$ P $后pRender 方式。下面是这个方法的自定义修改的一部分:
After that open the AjaxFileUpload class code and change the OnPreRender method. Here is a part of this method with custom modifications:
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"];
这是在服务器code,我们需要的所有更改。现在,我们需要修改 Sys.Extended.UI.AjaxFileUpload
客户端类(文件 AjaxFileUpload。pre.js )
That's all changes in server code we need. Now we need to modify the Sys.Extended.UI.AjaxFileUpload
client class (file AjaxFileUpload.pre.js )
首先让我们修改 _html5UploadFile
方法如下:
Firstly let's modify _html5UploadFile
method as below:
_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请求。
As you can see above, we adding contextKeys to form data, posted with Ajax request.
在我们需要修改 _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);
}
所有这些更改后,您可以在$ C $设置ContextKeys财产C-后面,把它从 AjaxFileUploadEventArgs
看重UploadComplete事件的参数如下:
After all these changes you can set ContextKeys property in code-behind and get it value from AjaxFileUploadEventArgs
argument of the UploadComplete event as below:
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
客户端事件的,你可以把服务器你contextKeys从客户:
Also, if you'll implement OnClientUploadStarted
client-side event as proposed here link, you may pass to server your contextKeys from client:
function uploadStarted(sender, args) {
sender.contextKeys = { "first": "1", "second": "2" };
}
这篇关于我怎样才能使用的AjaxFileUpload控制ContextKeys财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!