Primefaces file upload - file without extension - file-upload

I am using Primefaces v6.2.13 to upload a file. Works fine for files with extension, but crashes when trying to upload file without extension.
This is the part of xhtml used to render upload button
<p:fileUpload value="#{addFileController.file}"
mode="simple" skinSimple="true"
rendered="#{!addFileController.uploadDisable}"
label="Choose file"
allowTypes="#{addfileController.allowedTypes}" />
When I choose file, nothing happens, but as soon as I click the upload button, it throws an error, that I am unable to catch in my code, since it first calls some upload mechanism in PrimeFaces and later the code that I wrote.
The error is:
javax.faces.FacesException: File must have an extension
at org.primefaces.util.FileUploadUtils.getValidFilename(FileUploadUtils.java:54)
and the problematic part of the code inside class looks like this
...
String name = FilenameUtils.getName(filename);
String extension = FilenameUtils.EXTENSION_SEPARATOR_STR + FilenameUtils.getExtension(filename);
if (extension.isEmpty()) {
throw new FacesException("File must have an extension");
}
....
}
Any idea how to deal with this? I was thinking about adding some attribute to xhtml to disable uploading files without extension, since I don't want to edit PF source code.

Related

Is there a way to simulate a link/button click in mink?

I'm using a headless browser (phantomjs) in conjunction with Mink to do some functional testing on my Website.
Now in this setting, files can not be downloaded regularly e.g: by clicking a link. So I have to extract the url from the link or the button, and download the file manually.
As I just stated sometimes there is no link () for the download, but a button in a Form (e.g: Inputting data for a report in the form, and receiving the report file on submission).
So what I need to do is simulate clicking the link or button and extract the Data for the Request that would have been sent, and use that data to download the file manually.
Is there a way to do this?
Note: I'm using guzzle to actually download the file.
Mmmm... I don't know if you solved this and only as an alternative to typical mink methods. As Phantomjs is a javascript based browser engine, did you tried with javascript?
You could try something like this:
public function getElementHref($element)
{
/* #var FeatureContext $this */
$function = "(function(){
//Javascript method to get the href.
})()";
try {
return $this->featureContext->getSession()->evaluateScript($function);
} catch (Exception $e) {
throw new Exception('Element not found');
}
}
You can find a method to do this in javascript here: How to get anchor text/href on click using jQuery?
Then use the URL returned with file_get_contents (depending on the file type) and that's it.

Display PDF Content in an Aurelia View

I have some PDF files that I need to display in an Aurelia View.
The files are not available by direct link
I have an API function that returns a byte array.
In ASP.Net, I have some control over the response object's headers and content type, and can BinaryWrite the contents into the response.
I can't figure out how to do this in Aurelia.
Any suggestions?
Edit:
I'm attempting to use pdf.js as suggested. Injection in Aurelia fails.
import {inject} from "aurelia-framework";
import {HttpClient} from "aurelia-fetch-client";
import {PDF} from "pdfjs-dist"
#inject(Router, HttpClient, PDF)
export class PDFView {
constructor(router, http, pdf) {
this.router = router;
this.http = http;
this.pdf = pdf;
}
The console displays this error:
Inner Error:
Message: key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?
Inner Error Stack:
Error: key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?
at Container.get (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:480:15)
at Object.invoke (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:341:81)
at InvocationHandler.invoke (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:300:168)
at Container.invoke (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:564:25)
at StrategyResolver.get (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:127:37)
at Container.get (http://localhost:65397/jspm_packages/npm/aurelia-dependency-injection#1.0.0-beta.1.2.3/aurelia-dependency-injection.js:501:23)
at eval (http://localhost:65397/jspm_packages/npm/aurelia-templating#1.0.0-beta.1.2.7/aurelia-templating.js:3925:73)
End Inner Error Stack
------------------------------------------------
The import statement must be failing, yet pdfjs appears to load successfully as I receive status 200 for both pdfjs-dist#1.5.294.js and pdfjs when the page loads.
I can find no samples of Aurelia / pdfjs apps.
Repeating: I need to embed the stream as the PDF is not available via HTTP.
I'm not sure where to go from here
As far as I know there is nothing Aurelia-specific about loading pdf files.
You can use pdf.js. It has method getDocument that accepts binary data stored in byte array.
Then you can place a canvas inside your view, load pdf data during view activation and render pages into it using pdf.js after html is attached. For example code check answer by toddmo for this post: Pdf.js and viewer.js. Pass a stream or blob to the viewer

Why isn't my HTML file showing up in MVC 4 when it's in the Shared folder?

Given this link:
Test
Which is an HTML page in Shared folder as shown in the href attribute. Shouldn't MVC be able to find this in the Shared folder?
When I click on the link I get a 404 error.
Found the solution and am posting for others benefit...
MVC routing is wired up out-of-the-box to NOT handle inbound .HTML requests. So why fight it?
Create an action method that looks like this:
public ActionResult GetHTMLFile(string filename)
{
var dir = "/Views/HTML/";
var suffix = ".html";
var path = dir + filename + suffix;
return new FilePathResult(path, "text/html");
}
This tells us that we have a Views folder and a subfolder named HTML where we store all of our HTML files and ensure the suffix is always html. VS does this by default when you add new HTML files. This method is "building" the full path to the HTML file. It then uses FilePathResult to return the content.
Use the ActionLink helper because you can easily configure the action method name as well as the controller name if you need it...
#ActionLink("Layout File changes", "GetHTMLFile", new {filename="_Layout"})
We are "Tricking" MVC by only passing part of the file name (without the extension and without the actual path). The controller get's the string and serves up the HTML for us.

MVC 4: How to create visual Indicator while FileResult Action generates PDF to be sent back to browser?

I have FileResult Controller Action that can take a couple minutes to run under some circumstances. I would like to provide our users a visual indication that the Action is running, such as a message, a spinner, or a progress bar. The trouble is, I'm having a tough time figuring out how to "detect" on the front end that the Action has completed.
I have been toying with Ajax calls to the FileResult Action, but that doesn't work because Ajax can't return a file to the browser. I also looked at Asynchronous Actions and Tasks, but it looks like FileResult does not support the "await" keyword, which I think is necessary.
At this point, I don't know what to try next, and am very open to thoughts/ideas.
Edit:
I got this working as bobek suggested. Basically, I saved the PDF file to disk, and returned the file path as an ActionResult Content(fullFilePath). Then, on my page, I used something like this to display a little spinner gif while the Action executed, and then a link to the file after it completed.
var img = $("img#reportLoadingIcon");
var link = $('a#exportedFileLink');
link.hide();
img.show();
$.ajax({
url: url,
success: function (result) {
link.attr({target: '_blank', href: result});
link.show();
img.hide();
}
});
You could make it a regular POST action called via AJAX, and instead of returning a File you can return path to file and redirect the user to that path.

Primefaces FileUpload error remains even after correct upload

I am using primefaces3.5 fileupload control. When I upload a wrong format file then it shows a error message "Wrong format etc". After that when I upload a correct format file then it uploads fine but does not remove the error message.While searching I found this solution on primefaces forum but it is also not working.
How can I remove the error message on subsequent uploads?
Here is my code
<p:fileUpload id="fu"
allowTypes="/(\.|\/)(DOC|DOCX|doc|docx|ppt|pptx|xls|xlsx|pdf)$/"
onstart="loadingCursor();" oncomplete="simpleCursor();"
multiple="false" mode="advanced" sizeLimit="52428800"
showButtons="false"
fileUploadListener="#{documentInsertController.uploadPListener}"
label="Browse"
invalidSizeMessage="File size exceeds limit 45 MB "
value="#{documentInsertController.file}" auto="true"
invalidFileMessage="Invalid file type.Only doc,ppt,xls and pdf files allowed."
>
<h:message id="docMSG" for="fu"></h:message>
</p:fileUpload>
Worked for me with PrimeFaces 3.5:
<script type="text/javascript">
$(document).ready(function () {
fileuplaod_wgt.buttonBar.find("button.cancel").bind("click", function (e) {
clearInvalidFileMsg();
});
});
function clearInvalidFileMsg() {
fileuplaod_wgt.uploadContent.find("tr.ui-state-error").remove();
}
</script>
And I've added widgetVar="fileuplaod_wgt" in the p:fileUpload. Then cancel button works and removes invalid files.
It is not a permanent solution. Just a workaround until it will be fixed within PrimeFaces itself. Check: https://code.google.com/p/primefaces/issues/detail?id=3652