Heya all,
I'm working on a web-based application (using ASP.NET and C# on .NET
1.1.4322, supporting only IE6 for Windows) that allows for file uploads.
Screen real estate is at a premium, so I want to set its style to "display:
none;" and interact only with the standard file upload dialog (which can be
summoned by firing the click() method of the control).
Attempting programmatic access of a file upload control is generally playing
with fire, and this time is apparently no exception. Once I call the
click() method, it seems, the page is no longer able to post back. When I
attempt to submit the form, the value of the file upload control is cleared
and the page just sits there; no network traffic is generated as a result of
the attempted form submission. However, if I make the file upload control
visible and manually click its "Browse..." button, the page posts back
correctly.
For those of you familiar with Gmail's attachment upload interface, this is
very close to the effect I am trying to achieve, but something about ASP.NET
is apparently interfering (probably for my own protection) with the process.
Has anybody experienced similar behavior under the same platform, and if so,
explain the behavior or point to a KB article or security documentation that
might be enlightening ?
Thanks greatly for your time. Toy code follows.
R. Jones
ra*********@gmail.com
Codeahead:
==========
<%@ Page language="c#" Codebehind="Foo.aspx.cs" AutoEventWireup="false"
Inherits="FooApp.Foo" %>
<script>
function foo()
{
document.getElementById('<% = this.f.ClientID %>').click();
}
</script>
<body>
<form runat="server" id="form1" enctype="multipart/form-data">
<!-- just here to make it easier to tell when a roundtrip has occurred -->
<div id="b"></div>
<input runat="server" type="file" id="f">
<input type="submit" value="Submit">
<!-- programmatically summon file upload dialog -->
<input type="button" onclick="foo();" value="Upload file
programmatically...">
<!-- button to avoid "page has expired" message -->
<input type="button" onclick="window.location.href =
'http://localhost/Foo.aspx';" value="Clear">
<br>
Postback info:
<div runat="server" id="s" /> <!-- info provided on postback -->
</form>
</body>
<script>
document.getElementById('b').innerText = new Date().toString();
</script>
Codebehind:
===========
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace FooApp
{
public class Foo : System.Web.UI.Page
{
protected HtmlInputFile f;
protected HtmlGenericControl s;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
s.InnerText = String.Format("{0} file(s) posted", Request.Files.Count);
if (1 == Request.Files.Count) { s.InnerText += String.Format(", content
length: {0}", Request.Files[0].ContentLength); }
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}