3

我正在使用此处描述的 ajaxFileUpload:http ://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

它工作正常,除非我在同一页面上有多个文件上传控件。具体来说,我正在尝试为不同的问题上传不同的文件。当我在页面上上传第一个时,它工作正常,但页面下方的那个只会将它的文件上传到第一个问题的答案中。

我不确定这是否有意义......所以它可以帮助您知道我的页面是使用 ascx 文件动态填充的问题。文档 ascx 文件如下所示:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Document.ascx.cs" Inherits="ScholarshipApplication.controls.questions.Document" %>


<ajaxToolkit:AjaxFileUpload OnUploadComplete="UploadComplete"  ID="FileUploadControl" MaximumNumberOfFiles="1" runat="server" AllowedFileTypes="png,jpg,jpeg,pdf,tiff,tif,gif" />
<asp:LinkButton ID="downloadButton" runat="server" CausesValidation="false" OnClick="downloadButton_Click" />

以及背后的代码:

public void UploadComplete(object sender, AjaxFileUploadEventArgs e)
        {
            entry.data = e.FileName;
            entry.setDocumentData(e.GetContents());

            this.downloadButton.Text = e.FileName;
        }

我最初的想法是,我需要以某种方式帮助控件生成的 javascript 知道它应该在什么时候触发哪个问题。

4

3 回答 3

2

I believe this is a bug in control or this was implemented by some non-obvious reason. Actually, this control doesn't support multiple instances on a page. Consider to use AsyncFileUpload control instead or customize a bit sources of the AjaxFileUpload control. If you prefer second option then you need to download sources from here: http://ajaxcontroltoolkit.codeplex.com/SourceControl/BrowseLatest and change AjaxFileUpload.cs file (here is a path: /Server/AjaxControlToolkit/AjaxFileUpload/AjaxFileUpload.cs). What you need to do is to change ContextKey constant to property for combining context key guid with unique id of control:

public class AjaxFileUpload : ScriptControlBase
{
    private const string ContextKeySuffix = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";

    private string ContextKey
    {
        get { return this.UniqueID + "_" + ContextKeySuffix; }
    }

Actually, if you'll look on PreRender method of AjaxFileUpload class you'll easy realize reson for such behavior of this control (the first control handle uploads from all sibling controls on a page).

于 2013-01-03T22:10:20.507 回答
0

根据我的理解,您需要一个隐藏字段变量来识别您在 UserControl 中的问题 ID:

<input type="hidden" id="hdnQuestionId" runat="server"/>

在填充/生成问题时,您需要设置此变量,当您上传文档时,获取此隐藏值并使用它。

于 2013-01-03T05:12:51.793 回答
0

I created a data attribute named "data-upload-type" on ALL AjaxFileUpload controls and set it to the name of the type. Then I set up the client call to grab that value and set a cookie with the same value. The cookie IS received on the server side functions and I branch based on the value I receive.

Here is an example:

function StartUpload(sender, args) {
    var t = $(sender._element).attr('data-upload-type');
    document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';';
}

<asp:AjaxFileUpload ID="afuUploader1" runat="server"  OnClientUploadStart="StartUpload" OnUploadComplete="UploadComplete" OnClientUploadComplete="UploadComplete" data-upload-type="UploadType2"></asp:AjaxFileUpload>

Then in your server side upload call simply check Response.Cookies("upload-type"). Works like a charm!

于 2016-09-30T21:23:46.843 回答