Uploading File in WatiN - watin

How do I upload a file using WatiN?
Is it possible for this file to reside on a web server (as oppose to it being on user's machine)?
Code snippet is highly appreciated. Thanks.

The file needs to be accessible to the client browser. That means it either needs to be on the client machine or accessible through a share. If you want to store files in a central location, either use a shared folder, or come up with a way to copy the file to the client when you need it.
As for uploading the file, that is going to depend on how you have to do it. If it uses a standard file input tag, it would work like this:
HTML snippet:
<form action="upload.asp" method="post">
<input type="file" name="uploaded_file">
<input type="submit" name="submit_upload">
</form>
Code:
void UploadFile(string filepath, Browser browser)
{
FileUpload upload = browser.FileUpload(Find.ByName("uploaded_file"));
upload.Set(filepath);
Button submit = browser.Button(Find.ByName("submit_upload"));
submit.Click();
}

Related

How to save files after app migration to server

I migrated my app to server and after migration, my app doesnt save files in right folder. When I'm trying to upload them.
Its weird because my folders structure didnt change at all, since that was the only reason i supposted that might fail.
My class does save file but instead of public/storage/images it saves it inside storage/app/public/images.
Is this proper folder to save files?
Line of class code that stores files:
$event->photo_patch = $request->photo_patch->store('images', 'public');
Form that makes my images to load:
<label for="Photo">{{ __('Photo') }}</label>
<input type="file" class="form-control-file" name="photo_patch" accept="image/png, image/jpg">
#error('photo_parch')
<div class="invalid-feedback d-block">{{$message}}</div>
#enderror
{{ __('Best if image would be in 16:9 ratio') }}
</div>
Before server migration everything was saving in public/storage/images that's why I'm a bit shocked.
This is my first app that I'm developing and I'm not sure where files should be stored tho.

Cant download PDF with Ajax response (headers are ignored)

Im trying to download a pdf with ajax request yet the response from that request ignores my "Content-Type:application/pdf" header.
The ajax request
return Superagent.post(url, data).use(this._addDefaults.bind(this));
The symfony response
return new BinaryFileResponse($target);
Workaround
open in new tab and put a direct url for the file.
I think this is similar to a problem I've encountered in the past. Issue is that browsers don't open responses to POST ajax requests by default. I ended up resorting to the automatic form posting instead. In the HTML:
<form method="post" action="http://handler1.ashx" target="_blank">
<input type="hidden" id="data" name="data" value="post_body_stuff"/>
<button id="pdfsubmit" type="submit" value="Submit">Export to PDF</button>
</form>
You can use javascript to put all the data you want to POST as the value on the hidden input field instead of post_body_stuff. This way, when the content comes back the browser will try to open or save it as a file rather than read it as response text.

how to send file data using Dajaxice?

I am using ajax for my website. I have successfully used jQuery.ajax() to asynchronously upload file to server. I am using Dajax and Dajaxice therefore I plan to use these application for file upload as well. I tried this example. It is working fine. But if I add file field into my html form, it does not send file to server. My html form looks like
<form id="myform" action="/file/" method="post" enctype="multipart/form-data">
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='AaSmyBEwQLSD3YghRAD9Cf2uxEjzESUe' /></div>
<p><label for="id_docfile">Select a file</label> max. 42 megabytes</p>
<p><input type="file" name="docfile" id="id_docfile" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
This question has been asked at many place but never answered.
Afaik there is currently no provision within dajax / dajaxice to upload files.
I have used dajax in a few projects and have got round this by using blueimp/jquery-file-upload and a django view that accepts a POST of the upload file and return a JSON string to the client.
This is a less than perfect solution not least because the jquery-file-upload button is styled differently from normal html form elements, it is possible to style the whole form using jQuery-ui, although this is a lot of additional work.
Both, dwr which is pretty much dajax for Java, and tasty pie for django do offer file uploading, so in theory it should be possible to implement it.
I'm happy to post a sample of my ajax solution if anyone would find them useful.
I've also faced this problem recently. So, I've digged a little and discovered some answers.
It is working fine. But if I add file field into my html form, it does not send file to server.
There's serialize() method used in the doc example. But according to the jQuery doc:
Data from file select elements is not serialized.
Also, there's no clear way to get ajax file upload, because JS doesn't have access to the outside of the client browser. So, I don't think it's possible to make using dajaxice.
The easiest hack, I've found is to post form to the invisible iframe using target option:
<form method='POST' action='/upload' enctype='multipart/form-data' target='submit-iframe'>
so, only the iframe will be refreshed. Using js you than can get data from it catching the load() event.
More detailed process described here

Absolute url from relative path in an ascx file?

Imagine I have an image hosted at http://www.mypage.com/content/images/myimage.png and I want to display it on my webpage. In my ascx file is the following:
<img runat="server" id="ImgLarge" class="myimage" src="../content/images/myimage.png" />
The only problem is that this image doesn't print. I believe the fix is to have the image use the absolute URL but I'd rather not use http://www.mypage.com/content/images/myimage.png and instead use something like src=#Url.Absolute('~/images/solar-panel.png').
Does this work in ascx files? If not, is there an alternative? I've only ever done this in my cshtml files. Thanks!
Is the image a part of the project? Typically you would have your user controls in one folder and image controls in another (same level). If this is the case, you would have a url like this:
<asp:ImageButton runat="server" ID="ImageButton2" OnClick="sortApproved_Click" ImageUrl="~/images/down.png" />
Other wise, if you want to diagnose whats going on, look at the rendered web pages source. What does the url look like?

Upload file to remote server using ajax

I don't have any control on the server side..
is it possible to upload and load the results given by the remote server in an Iframe ?
please share some code..
Thanks
Declare the iframe with a name and target that name in your form element:
<form action="http://url.to.server" enctype="multipart/form-data" target="resultsFrame">
<input type="file" />
...
</form>
<iframe src="blank.html" name="resultsFrame"></iframe>

Resources