File upload using ng-file-upload in spring mvc - angularjs

I used ng-file-upload for uploading a file, but in my spring mvc controller with #RequestPart MultipartFile, the getOriginalFilename() method returns blob not the file name
#RequestPart(value = "imageFile", required = false) MultipartFile imageFile
The console log for my upload is:
{$ngfName: "UI-UX.jpeg", $ngfOrigSize: 133046, size: 6320, type: "image/jpeg"}
My file uploads correctly but I don't know why the file name is not correct.
Can you please help me?
Thanks.

Try with this
#RequestParam("image") MultipartFile image

Related

Transform img from url to file in React

I have an autocomplete section that searches movies through MovieDB api.
The result of the api has a field called poster_path that contains the path where the image exists.
"poster_path": "/lRQiJXETkCnVVurHmglNvMXrZOx.jpg"
I have a react project for front-end and a spring boot project for backend.
The thing I want to do is to get the image convert it to file and the send it to the backend controller as a multipart file.
How can I convert the image from url to file in React?
I've tried this but re.data returns the byte array I think:
axios.get(e.image).then(re => {
setFile(re.data)
});
And here is the controller:
public ResponseEntity<Post> post(#RequestPart(required = false) MultipartFile file, #RequestPart #Valid PostResource postResource, Principal principal)

How to upload a local json file using Blazor

I am trying to select a local json file and load it in my blazor client component.
<input type="file" onchange="LoadFile" accept="application/json;.json" class="btn btn-primary" />
protected async Task LoadFile(UIChangeEventArgs args)
{
string data = args.Value as string;
}
P,S I do not understand , do i need to keep track both the name of the file and the content when retrieving it ?
I guess you're trying to read the contents of a JSON file on the client (Blazor), right? Why not on the server !?
Anyhow, args.Value can only furnish you with the name of the file. In order to read the contents of the file, you can use the FileReader API (See here: https://developer.mozilla.org/en-US/docs/Web/API/FileReader). That means that you should use JSIntrop to communicate with the FileReader API. But before you start, I'd suggest you try to find out if this API have been implemented by the community (something like the localStorage, etc.). You may also need to deserialize the read contents into something meaningful such as a C# object.
Hope this helps...
There is a tool that can help, but it currently doesn't support the 3.0 preview. https://github.com/jburman/W8lessLabs.Blazor.LocalFiles
(no affiliation with the developer)
The input control will give you the location of the file as a full path along with the name of the file. Then you still have to retrieve the file and download it to the server.
Late response but with 3.1 there is an additional AspNetCore.Components module you can download via NuGet to get access to HttpClient extensions. These make it simple:
// fetch mock data for now
var results = await _http.GetJsonAsync<WellDetail[]>("sample-data/well.json");
You could inject the location of the file from your input control in place of the "sample-data/well.json" string.
Something like:
using Microsoft.AspNetCore.Components;
private async Task<List<MyData>> LoadFile(string filePath)
{
HttpClient _http;
// fetch data
// convert file data to MyData object
var results = await _http.GetJsonAsync<MyData[]>(filePath);
return results.ToList();
}

Swagger UI how to display upload of multiple files

I've a Spring MVC application that is wrapped with Swagger annotations.
<spring.version>4.1.8.RELEASE</spring.version>
<java-version>1.7</java-version>
<springfox-version>2.1.2</springfox-version>
I've a controller with two resource endpoints to allow file upload - one endpoint for a single file upload and other with multiple file upload.
Single file upload -->
public #ResponseBody UploadAPIResponse postInboundFile(
#ApiParam(value = "file to upload") #RequestParam("file") MultipartFile file,
#ApiParam(value = "Inbound trigger flag ") #RequestParam("triggerInbound") Boolean triggerInbound)
throws SQLException, Exception {
Multi file upload -->
public #ResponseBody BulkUploadAPIResponse postInboundFilesAsync(
#ApiParam(value = "files to upload") #RequestParam("files") MultipartFile[] files,
#ApiParam(value = "Inbound trigger flag ") #RequestParam("triggerInbound") Boolean triggerInbound,
HttpServletRequest request) throws SQLException, Exception {
On viewing the Swagger UI for the api documentation,
I see that for single file upload API, against the "file" parameter, I get a file upload button.
However for the multi file upload API, I get the "files" parameter as a text box.
Can someone pls help me how can I get a file upload button against the "files" parameter of multi-files upload API and I'm able to upload multiple files as part of single request ?
I've gone through various other posts on this context but not able to find a concrete approach to address this issue.
PS : I've the understanding that naming the parameter as "file" provides the file upload button automatically through Swagger-UI. I tried to use the parameter name as "file" instead of "files" for the multi-file upload API but that didn't help; i still end up getting a text box (probably reason being that argument is an array of MultipartFile).
Thanks in advance for any pointers/suggestions.

Convert base64 data url to image file in angularjs

I have base64 data URL in my angularJs controller, and I need an image file from that, so that I could send it to server as multi-part data through ajax?
I'm looking for something like file writer in angularjs.
Can anyone help me please?
you can generate blob from base64 data.
var imageBase64 = "image base64 data";
var blob = new Blob([imageBase64], {type: 'image/png'});
From this blob, you can generate file object.
var file = new File([blob], 'imageFileName.png');
You can use this file object and post it to server using multipart data.

Uploading and storing file with Laravel

I am using Laravel illuminate/html and I am trying to upload an image file and store it in /public in the laravel installation folder. I have got the image from the request:
$img = Request::file('img');
How can I store it in the public folder?
Thanks
You could do this in your controller:
Request::file('img')->move(base_path('public/uploads'));
Or if you wish to specify a generic filename or change filename
$newfilename = str_random(32) .time();
$ newfilename = $newfilename. ".". Request::file('img')->guessClientExtension();
Request::file('img')->move(base_path('public/uploads'), $newfilename);`

Resources