Javi
Javi

Reputation: 81

FileUpload watin

I need to upload a file in a website using watin. The problem is that setting the direction of the file, like this:

browser.FileUpload(Find.ById("ctl00_cpContent_FileUpload1")).Set(DIRECCION_XML + "plantilla.txt");

doesnt work. Because this, I need to handle the windows popup that appear and fill the direction of the file to upload. I dont know how to do it... I were searching info of FileUploadHandler, but i cant get it.

There is more option than that? Pls, help me with a possible code to do it.

Really thanks

Upvotes: 1

Views: 1037

Answers (2)

ProgrammerV5
ProgrammerV5

Reputation: 1962

The FileUploadHandler works great. I have it running in production mode with thousands of files being uploaded every day and I haven't had any issues with it so far.

This is the way it needs to be implemented:

EDIT: (I forgot to include the uploadDialog object)

IntPtr hwndTmp = (IntPtr)FindWindow("#32770","Select file(s) to upload"); // or whatever the window text says when you are opening that upload window)
Window uploadDialog = new Window(hwndTmp);

UploadFileDialogHandler uploadFile = new UploadFileDialogHandler(_toBeSent.FileToSent);
_browser.AddDialogHandler(uploadFile);
uploadFile.HandleDialog(uploadDialog);
uploadFile = null;

That will take care of the upload process. when you need to upload the file just those lines will take care of everything (loop thru all open dialogs, find the right one, find the text field, enter the name for you and click the Ok button. On top of that you need to create another class that will be the UploadFileDialogHandler:

public class UploadFileDialogHandler : BaseDialogHandler
    {
        private const int WmSettext = 0x000C;

        private string fileName;
        private bool _processed = false;
        public override bool HandleDialog(Window window)
        {
            var button = GetOpenButton(window);
            if (button != null)
            {
                if (_processed == false)
                {
                    var fileNameHandle = NativeMethods.GetChildWindowHwnd(window.Hwnd, "Edit");
                    var fileNameHwnd = new Hwnd(fileNameHandle);
                    fileNameHwnd.SetFocus();

                    _processed = true;
                    //MessageBox.Show("About to send " + fileName);

                    fileNameHwnd.SendString(fileName);

                    button.Click();
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public UploadFileDialogHandler(string file)
        {
            fileName = "";
            fileName = file;
            //MessageBox.Show("Setting filename: " + fileName);
        }

        public override bool CanHandleDialog(Window window)
        {
            return GetOpenButton(window) != null;
        }

        private WinButton GetOpenButton(Window window)
        {
            var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "&Open").FirstOrDefault();
            if (windowButton == null)
                return null;
            else
                return new WinButton(windowButton.Hwnd);
        }
    }
}

You can just copy and paste that class inside your program and with the 4 lines of code above it will take care of the rest for you. In case you need more information there's a good amount of information on the WatIn file source code but it could be a little bit challenging to follow if you don't understand the Windows API.

Hope this helps.

Upvotes: 1

LTU
LTU

Reputation: 205

This command work for me fine: browser.FileUpload(Find.ById("FormImage")).Set("C:\\Pictures\\11.PNG"); Try it

Upvotes: 0

Related Questions