MVC big file download - ajax

I am using ASP.net MVC 4.0. I want to download a file after clicking a download button/a link. The problem is that the file is big and I want to show a 'wait image' while downloading. How do i do it? I am getting the file as a stream? Should I use HTPResposneMessage with web-api or FileStreamResult with MVC? The issue is I want to be notified when the download finishes.
My file is being downloaded with a code as below:
public FileStreamResult Download(Guid id)
{
.......
return this.File(cab, "application/octet-stream", id + ".cab");
}
I want to show a spinner in javascript before beginning the download. My code is as follows:
self.IsDownloading(true);
var url = '/Download/' + id;
window.location = url;
self.IsDownloading(false);
But IsDownloading(false) is being executed before downloading the full file. How to make sure that it is done after the full file is downloaded?

Related

Problems automatically converting to .txt when downloading xlsx files (excel files) within a Java (SpringBoot) server

When downloading a .xlsx file on a server, it automatically converts to .txt when asked for a save path.
You are currently working with SpringBoot, and if you click the download link through the tag in the view, click the .xlsx file.
The method of loading from the specified path.
The problem is that if you specify the href path for tag a as resource/static within the project, the .xlsx file will be downloaded without any problems.
However, if you route to a folder outside the project, the .xls file is downloaded normally, but. The xlsx file is automatically converted to .txt.
The path imported from the external folder is in the form of a request to the controller to return the actual file.
Below is the code that returns the file from the controller.
I'd like to ask those who know about this problem.
#GetMapping
public byte[] file(HttpServletRequest request) throws Exception {
FileInputStream in = new FileInputStream(new File().getPath() + "/filemanager/"
+ request.getRequestURI().split(request.getContextPath() + "/filemanager/")[1]);
byte[] image = IOUtils.toByteArray(in);
in close();
return image;
}

How to save a file to downloads using WinUI 3?

How do I save a file generated by my application to the downloads folder when using the WinUI 3 SDK? I know I can use FilePicker to select a file to use in my application but I would like to essentially do the opposite and save a file from my application somewhere or just simply put it into downloads.
This is an example saving the StoreLogo.png file, which is in the Assets folder by default, to the Downloads folder.
using Windows.Storage;
...
// Get the StoreLogo.png file as a StorageFile.
Uri storeLogoUri = new($"ms-appx:///Assets/StoreLogo.png");
StorageFile storeLogoFile = await StorageFile.GetFileFromApplicationUriAsync(storeLogoUri);
// Get the Downloads folder as a StorageFolder.
string downloadsFolderPath = UserDataPaths.GetDefault().Downloads;
StorageFolder downloadsFolder = await StorageFolder.GetFolderFromPathAsync(downloadsFolderPath);
// Save (copy) the StoreLogo.png file.
await storeLogoFile.CopyAsync(downloadsFolder);

Uploading images to Windows Server and making them available publicly

I am trying to upload images in my windows server VM "hosted on-premise" and make these images available publicly like www.example.com/imageFolder/cat.png.
This VM has IIS enabled (I am not sure if IIS is related here) and it is assigned a URL and available publicly.
Aside from any programming languages or frameworks, I just want to know if storing the image in the server and making it available via a link is possible? If so, how can I achieve it?
To clarify further:
I believe what I want is very simple and tedious.
Consider the following workflow:
Run an API that has end points to receive images on the server.
Store the messages in the server.
Return the link that points to the picture.
I want to know the procedure from the windows server side like
Do I need to set certain properties on the folder where I will store the images?
Do I need to add a site to show the images back to the user?
I am a developer and I am new to the Windows Server & IIS so I may not have all the fundamentals.
I made a sample by using ASP.NET MVC5. You can follow this article to upload image in MVC5 application.
Upload Images on Server Folder Using ASP.NET MVC
Then, I do some changes so that it can display the url of images..
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
Uri requestUri = HttpContext.Request.Url;
string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
if (ModelState.IsValid)
{
try
{
if (file != null)
{
string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName));
file.SaveAs(path);
}
string link = baseUrl + "/Images/" + file.FileName;
ViewBag.FileStatus = "File uploaded successfully.";
//Return the url of image
ViewBag.FileLink = link;
}
catch (Exception e)
{
ViewBag.FileStatus = "Error while file uploading. Error message is "+e.Message;
}
}
return View("Index");
}
Index.cshtml add this to show url.
<div class="col-md-offset-2 col-md-10 text-success">
#ViewBag.FileLink
</div>
Don't forget to add a folder named Images to store images. After publishing, you also need to add it in published folder, otherwise error message will show that cannot find folder.
It works well.

Laravel response helper file() method downloading, not displaying file

I am trying to display a file stored in storage folder using laravel's response helper. Unfortunately, every time it runs, the file is DOWNLOADED, not DISPLAYED. Below is my code:
public function viewDoc($id)
{
$a = pro_doc::findorfail($id);
$path = storage_path('documents/'.$a->file_name);
if ( ! File::exists($path) ) {
abort(404);
}
return response()->file($path);
}
Im sending an id to the controller, which looks up the record and gets the file name, then attempts to open the file in a new window (this is from a link with a blank target). A new window momentarily opens, then the download begins and the window closes.
The documentation (https://laravel.com/docs/5.4/responses#file-responses) seems to say this shouldn't happen
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
Can anyone help me make this work? Again, Im trying to get it to open in the browser window, not download. Thank you!

How to return pdf document from controller with ajax call in MVC app

I have an #Html.Action link that currently works for returning the user a pdf document from the controller. I want to be able to use an ajax call to perform the same function but I'm stuck on how or even if this can be done. I have tried several different iterations but I never get the pdf to download from the browser window. Here is what I have so far:
$('#Button1').click(function () {
var poNum = "51970";
$.ajax({
type: "GET",
data: "id= " + poNum,
url: '#Url.Action("PoReport", new { controller = "LogisticsTools"})'
});
});
I can copy the Request URL from the Headers window in Chrome Dev Tools and paste it into a new page and the pdf is downloaded. I can see the pdf code in the Response window also but it just doesn't get downloaded when the button is clicked. What am I missing?
You don't want to ajaxify this. Put the URL in an anchor tag and let the browser do the rest. If the browser doesn't recognise the document type or it is configured to force file downloads, the file will download as you expect (i.e. the user will see the download dialog). If the document is recognised, can be opened in the browser, and the browser is configured to open files, the file will open in the browser.
<a href='#Url.Action("PoReport", new { controller = "LogisticsTools"})'
title="Click to Download/Open">
Download
</a>
You can't download PDF file while using the ajax request to server. So instead of that you should use html actionlink. for example
#Html.ActionLink("Convert Data into PDF", "PDFView", null, new { #class= "btn btn-info" });
Above code will generate a link button and you can access Action Method of controller. Also you can use any technique to return PDF file from controller for example you can return PDF file using FileResult class.

Resources