Posting a File using the WebBrowser Class

February 12th, 2009 Matt J. Wilson 1 comment

So you have a WebBrowser object in .NET that you would like to use to automatically post a file to a given location?  You can probably figure out how to set values on this page:

<input type="text" name="myTextField">
// Update myTextField with a value
webBrowser.Document.All["myTextField"].SetAttribute("value", "I set your value");

But how do you update this?

<input type="file" name="myFileField">

You can’t simply use the SetAttribute function, as there is no attribute to correctly set; this was done intentionally to prevent users from using shady Javascript to automatically capture and send files from a page.  So how do you do it?  Something even more shady:

HtmlElement element = webBrowser.Document.GetElementById("myFileField");
element.Focus();
System.Threading.Thread.Sleep(1000);

// Perform SendKeys to fake the browser into setting the file name
SendKeys.SendWait(@"C:\Path To My File\test.txt");
SendKeys.Flush();

I hope that Google picks this up and saves someone else the thirty minutes worth of their life that I wasted attempting to figure out a solution.

Categories: Programming Tags: , ,

Internet Explorer Toolbar Frustrations

February 10th, 2009 Matt J. Wilson No comments

I recently developed a framework for Internet Explorer toolbars and am currently attempting to do the same based on some FireFox toolbars I have written.  During this development, I used several tutorials available on the web and found myself with a repeating error on any non-development machines:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

It turns out that a reference I was adding to the SHDocVW assembly was done incorrectly.  I simply looked at some sample projects and saw the reference pointing to the project’s “obj” directory, but this was incorrect!  The original reference should be added from your system32 directory.

An easy to way to diagnose if this is what’s happening to you:

  • Expand the “References” folder of your project
  • Look for a reference to SHDocVw
    • If this reference is exactly “SHDocVw”, this solution won’t fix your problem
    • If your reference is exactly “Interop.SHDocVw”, then remove the reference and add the one from your system32 directory

Inno Setup – The setup files are corrupted. Please obtain a new copy of the program.

February 10th, 2009 Matt J. Wilson No comments

During some recent work using Inno Setup to create a bootstrap application, several workstations were presented with the following error:

error 

 After hours upon hours of Googling, I still couldn’t find a fix that worked for me.  Upon more carefully examining my script, I noticed I had left the following line in my ISS file:

UninstallDisplayIcon={app}\icon25x25.ico

The problem was due to the fact that I never included this file under the [Files] area as I simply had no intention of ever using the icon.

So for future reference (and in case someone stumbles upon this through Google), always ensure any file reference is valid.  Your compiler and development machines may not throw an error, but if you plan to distribute this to several workstations this error may rear its ugly head.

UPDATE:

After fixing this file, I was still noticing an error on the hosting site (although any EXE file downloaded from my website worked flawlessly).  It turns out Redhat and PHP often don’t play too well together in this regard and need a few settings tweaked in httpd.conf, you may need to disable the EnableMMAP and/or EnableSendfile options.

Categories: Programming Tags: