dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Change programmatically IHTMLInputFileElement file

  1. #1
    Join Date
    Jan 2009
    Posts
    270

    Change programmatically IHTMLInputFileElement file

    I tried for several days to programmatically put a file path into a input type file element into a CHtmlView control (IE). I have tried this:
    Code:
    HTMLInputFileElement* pInputElem = NULL;
    hr = pElemDispatch->QueryInterface(IID_IHTMLInputFileElement, (void**)&pInputElem);
    CString sFile(_T("C:\\Flaviu\\SomeFile.txt"));
    BSTR bstrFilesAttribute = sFile.AllocSysString();
    hr = pInputElem->put_value(bstrFilesAttribute); // hr is S_OK though
    SysFreeString(bstrFilesAttribute);
    but didn't work, I mean there is no SomeFile.txt chosen on input file.

    I have read on several sites that I cannot edit or set value on an input type file element for security reason.

    Somewhere, someone, suggested a simple solution: to make a copy of the original web page, and edit html source code and replace input type file element to an input type text. Which I have done. I only have to solve "action" attribute on form:

    Original html source code:
    Code:
    <form name="uf" method="POST" action="/WS6ERT/SomeFile.do" enctype="multipart/form-data">
    <div style="background-color: #c6c6c6; padding: 5px">
        <span>Choose file:</span> <span> <input type="file" name="linkdoc" size="75" value=""></span><span><input type="submit" value="Send"></span>
    </div>
    </form>
    and I made a copy, and I replaced input type file, with input type text:
    Code:
    <form name="uf" method="POST" 
    action="https://originalsite.com/WS6ERT/SomeFile.do" enctype="multipart/form-data">
    <div style="background-color: #c6c6c6; padding: 5px">
        <span>Choose file::</span> <span> <input type="text" name="linkdoc"> 
    </span><span><input type="submit" value="Send"></span>
    </form>
    The important tags attribute: "action=" Of course that is not correct what I have written in my html source, because my path is pointing on the original file, which I didn't edit...

    Is there a way to solve this issue? To put programmatically a file path into an input type file on a CHtmlView view? Or how to overcome this problem? I will be more than happy if I solve this ...

    Last Edit: Can I setup the file with javascript ? I saw here: https://developer.mozilla.org/en-US/...ile#attr-files that is possible to setup a file in files attribute. But I don't know how to do that ...
    Last edited by mesajflaviu; November 27th, 2018 at 06:06 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,843

    Re: Change programmatically IHTMLInputFileElement file

    Not at all clear on what you are trying to do. Are you trying to have a user upload a file? Are you trying to display a file on a web page? Something else?

  3. #3
    Join Date
    Jan 2009
    Posts
    270

    Re: Change programmatically IHTMLInputFileElement file

    Sorry for not so clear post. It is mean a lot for me to solve it, and every time when I come here I get a solution, in a way or another.

    My task is to navigate to a financial site, and setup a file path to a input type file html control. Programmatically. And after that, I should click (programmatically) to "Send" button, and after that, the server response me giving me another page, from where I should take a number, as response to my sending file.

    The problem is that the page where I should put the file which should be sent has a input type file html control. Something like that: https://viljamis.com/filetest/

    I have tried to programmatically put a file with code:
    Code:
    HTMLInputFileElement* pInputElem = NULL;
    hr = pElemDispatch->QueryInterface(IID_IHTMLInputFileElement, (void**)&pInputElem);
    CString sFile(_T("C:\\Flaviu\\SomeFile.txt"));
    BSTR bstrFilesAttribute = sFile.AllocSysString();
    hr = pInputElem->put_value(bstrFilesAttribute); // hr is S_OK though
    SysFreeString(bstrFilesAttribute);
    has not worked.

    And now, I heavily seeking any workaround to solve his:
    Trial 1:
    Navigate with my CHtmlView with a clone page where I replaced the original input type file with input type text (see the first post). But even if I setup my text inside this input type text, I could send it with my html form due to "action" attribute from the html form (my first post detail that).
    Trail 2:
    I am trying to use this: https://docs.microsoft.com/en-us/cpp...17#sendrequest, where I could send what I want to the server, but in this way, I cannot get the server answer (the number as response from my uploaded file).

    I am thinking day and night how to solve this ... and I found nothing ...

  4. #4
    Join Date
    Jan 2009
    Posts
    270

    Re: Change programmatically IHTMLInputFileElement file

    I succeeded into put a text there, I attached the sample project.FFF.zip

    Go to Edit menu, and choose "Insert" item. You will see a file path on browse html control. But if I click "Send", the server respond me that I send nothing ... Most important: this is working only if you have in clipboard a text, don't matter what text (I don't know why is that).

    But even so, when I am trying to send the file, the server respond me that I uploaded nothing ! I don't get it ! Strange ...
    Last edited by mesajflaviu; November 28th, 2018 at 09:01 AM.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,843

    Re: Change programmatically IHTMLInputFileElement file

    Not an expert, but the input element has different types. As you know, it has the text type and file type (to name a couple).

    When the type is set to file, it functions as a control which allows the user to select and upload a file or files. In this mode, it appears that a user has to initiate the upload by clicking on the Choose File button. This causes the control to display a file open dialog where the user chooses a file). When the user closes the file open dialog, the file path is internally tracked and the file is uploaded (i.e. copied) to the web server. From there, the file name, attributes, bytes are uploaded to the web server as a byte stream where they can be saved. At no time does the server know or track the original file location.

    Given all that is going on, imo you aren't going to be able to simply set the input type to text and expect the file path you set to be uploaded as a file. For one thing, setting the input type to text rather than file makes the input control work like a text control instead of a file upload control. Setting it to text allows you to set the text, but nothing else will happen (like what to do with the text).

    So I don't see what you are trying to do as possible and if it were, it would be a security issue.

    Rather than attempt to upload a file in this manner, can you use another approach (like is there an ftp server available)?

  6. #6
    Join Date
    Jan 2009
    Posts
    270

    Re: Change programmatically IHTMLInputFileElement file

    Very good idea. I have tried and it worked. Thank you Arjay !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)