4

非常坦率的。我只是在寻找能够在上传之前为文件添加标题的用户。(是的,我鼓励使用正确的文件名,但这不是重点。)

<asp:TextBox runat="server" ID="txtDocumentTitle" />
<ajaxToolkit:AjaxFileUpload runat="server" ID="ajxUploadNDA" OnUploadComplete="ajxUpload_Complete" Width="400px"   /><br />

    protected void ajxUpload_Complete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        MyFile f = new MyFile();
        f.DocumentType = e.ContentType;
        f.FileBytes = e.GetContents();
        f.FileName = e.FileName;
        f.DocumentCategory = "Package Files";
        f.FileUploaded = DateTime.Now;
        f.DocumentTitle = txtDocumentTitle.Text;
        f.Save();

        DataBind();
    }

但是,在设置断点时,txtDocumentTitle.Text 始终为空白。我似乎无法强制进行完整的回发或找到任何其他方式来获取该文本框的当前值。我可以允许用户在文件上传编辑这些属性,但这不是我喜欢的设计,原因有几个。(它鼓励将值保留为默认值。)

我试过了:

    protected void Page_Init(object sender, EventArgs e)
    {
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(ajxUploadNDA);
        ScriptManager.GetCurrent(Page).SupportsPartialRendering = false;
        ScriptManager.GetCurrent(Page).EnablePartialRendering = false;

    }

我试过了

<ajaxToolkit:AjaxFileUpload runat="server" ID="ajxUploadNDA" OnUploadComplete="ajxUpload_Complete" Width="400px" onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"  />

任何建议都会受到欢迎。

4

4 回答 4

1

I've sort of solved it by adding a button to "Set Document Title" which adds the value of the textbox to session. The ajxUpload_Complete function then uses this Session variable to set the title to that session value on upload.

It's sloppy for a couple reasons, but it's the best I could do.

On Page_Load:

if (!Page.IsPostBack && !ajxUploadNDA.IsInFileUploadPostBack)
{
  Session.Remove("DefaultDocumentCategory");
  lblDocumentCategory.Text = "Data Package Files";
  Session.Remove("DefaultDocumentTitle");
  lblDocumentTitle.Text = "Data Package File";
}

protected void btnChangeDocumentAttributes_Click(object sender, EventArgs e)
{
    lblDocumentCategory.Text = cboDocumentCategory.SelectedValue;
    lblDocumentTitle.Text = txtDocumentTitle.Text;
    Session["DefaultDocumentCategory"] = lblDocumentCategory.Text;
    Session["DefaultDocumentTitle"] = lblDocumentTitle.Text;
}

I also added a dummy button to the page to force a postback refreshing my gridview that shows all the files uploaded.

<asp:Button ID="btnForcePostBack" runat="server" Text="" Style="background-color: Transparent; color: inherit; border-style: none;" />

protected void ajxUpload_Complete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{

    MyFile f = new MyFile();
    f.DocumentType = e.ContentType;
    f.FileBytes = e.GetContents();
    f.FileName = e.FileName;
    f.FileUploaded = DateTime.Now;

    if (Session["DefaultDocumentCategory"] == null || Session["DefaultDocumentCategory"].ToString() == string.Empty) f.DocumentCategory = "Data Package Files";
    else f.DocumentCategory = Session["DefaultDocumentCategory"].ToString();
    if (Session["DefaultDocumentTitle"] == null || Session["DefaultDocumentTitle"].ToString() == string.Empty) f.DocumentTitle = "Data Package File";
    else f.DocumentTitle = Session["DefaultDocumentTitle"].ToString();
    f.Save();
    ajxUploadNDA.Page.ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script type='text/javascript'>var btn = window.parent.document.getElementById('btnForcePostBack');if (btn) btn.click();</script>");
}
于 2012-10-10T00:15:37.130 回答
1

我无法得到任何其他答案。我最终将文本框放在了 ajax 更新面板上。然后我为文本框 OnTextboxChanged 创建了一个事件,该事件将值存储在会话中。然后我可以直接从会话中获取 UploadComplete 中的值。

于 2014-04-04T17:16:58.753 回答
0

使用 ajax 上传时,您只能立即保存,然后第二步是单独调用从保存的位置获取文件并对其进行操作。我在使用 Uploadify 和 Uploadifive 进行多次异步上传时遇到了同样的问题。上传多个文件时,我的第一步是保存到临时位置,然后进行第二次调用以检索它、调整大小并将其保存到云(Azure 存储)。不可能设置断点,因为线程到处都是。这是一种奇怪的行为,特别是在上传单个文件时,但这是首先保存然后使用单独调用检索的最佳解决方案。

于 2012-10-03T20:33:49.073 回答
0

问题是 AjaxFleUpload 控件使用隐藏框架来提交文件内容。您可以使用下面的脚本将文本框值传递给服务器:

Sys.Application.add_load(applicationLoadHandler);

function applicationLoadHandler() {
     var originalCreateForm = Sys.Extended.UI.AjaxFileUpload.prototype._createVForm;

     Sys.Extended.UI.AjaxFileUpload.prototype._createVForm = function () {
          originalCreateForm.call(this);

          var textBox = document.createElement("INPUT");
          textBox.setAttribute("type", "text");
          textBox.setAttribute("name", "<%= txtDocumentTitle.UniqueID %>");
          textBox.setAttribute("value", document.getElementById("<%= txtDocumentTitle.ClientID %>").value);

          this._vForm.appendChild(textBox);
     }
}

Request.Form在服务器上,您可以从集合中获取用户输入:

var title = Request.Form[txtDocumentTitle.UniqueID];
于 2012-10-08T08:50:14.200 回答