0

I'm looking for a way to add a string as a file inside a html form using JavaScript.

Now I know about the security issues the browsers have when it comes to files, but I'm not looking for adding a file path (as happens when you click the browse of a file input)!!
I want to add a string as a file (The server I'm hitting expects the post to include a file and not a simple form entry).

I know how to perform the action using the FormData object, but I don't want to generate a xhr(ajax) post. I need the post to be a simple form post since the server responds with a redirection.

Does anyone have an idea about this?

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • PHP would be able to create a fake file for you and do the post call through CURL. – Matt Cowley Jan 30 '17 at 13:26
  • What file type does it expect? – epascarello Jan 30 '17 at 13:27
  • I need to do this using html + javascript. The file type is a simple text file. The content is a base64 string. – Amir Popovich Jan 30 '17 at 13:27
  • So did you search and file questions like this: http://stackoverflow.com/questions/31048215/how-to-create-txt-file-using-javascript-html5 Does it help? – epascarello Jan 30 '17 at 13:32
  • I did search SO and didn't find a suitable result. – Amir Popovich Jan 30 '17 at 13:35
  • I don’t think this is possible. You won’t get a simple HTML form to send all the right headers and the data in the right format unless it contains an actual file input. – CBroe Jan 30 '17 at 14:01
  • @CBroe - I also can't think of a way to do this. I can post a file with a FormData object and an xhr request - that's what made me think that there may be a to create a non xhr post request. – Amir Popovich Jan 30 '17 at 15:08
  • No, not really. You can not “insert” FormData back into a normal form or anything like that. // I am not really sure what you need/want to implement here anyway. If the remote server requires a file uploaded by the client - then what sense does it make to go through the process without having the actual user select a file in their browser in the first place? What do you need this address the server redirects to for - if you don’t seem to be following the intended use here in the first place? – CBroe Jan 30 '17 at 15:21
  • @CBroe - Its kinda of a application deeplink that wants to receive information as a file (very weird). My intention is to create a form with a post request and perform the deeplink dynamically with a single click (the server also plants custom cookies - That's why I need the browsers help). – Amir Popovich Jan 30 '17 at 16:37

0 Answers0