i want to open a local PDF file in my sapui5 app, when i click a button.
Have anyone an idea, how could I solve this.
onOpenDoku: function(oEvt) {
//alert("Test für Dokumentenanzeige");
var pdfViewer = new sap.m.PDFViewer();
this.getView().addDependent(pdfViewer);
//var oSample1Model = new sap.ui.model.json.JSONModel({
//Source: sap.ui.require.toUrl("file:///C:/Test/TestDatei.pdf")
//});
//this.byId('DOKU_BUTTON').setModel(oSample1Model);
//var sSource = oEvt.getSource().getModel().getData().Source;
var sSource ="file:///C:/Test/TestDatei.pdf";
pdfViewer.setSource(sSource);
pdfViewer.setTitle("My Custom Title");
pdfViewer.open();
//this.onShowDoku();
},
You cannot read local file on client side because of security reason. You can try adding link to file or iframe.
PDF
or
<iframe src="file:///C:/Test/TestDatei.pdf" width="200" height="200"></iframe>
Related
I need access to files that are in a files server in my LAN from my Angular app.
I assume that I need to publish my Angular app in the same network, that is, in my IIS Server inside the same LAN
Now on my local machine, I try to access my shared folder \192.168.100.7\OfertasHistoric" but I don´t know how to do it.
When I try this
[HttpGet("directorio")]
public async Task<ActionResult<string[]>> GetDirectoryContents()
{
string[] files = Directory.GetFiles(#"\\192.168.100.7\ofertashistorico");
return files;
}
I get this error
System.IO.DirectoryNotFoundException: Could not find a part of the path '/Users/kintela/Repos/Intranet-WebAPI/Intranet.API/\192.168.100.7\ofertashistorico'
It seems that the path that you give to the GetFiles method only searches from the current directory where the project is located downwards and I don't know how to indicate a different one.
I also do not know how to manage the issue of the credentials necessary to access said resource
Any idea, please?
Thanks
I am using below code and it works for me. Please check it.
Steps:
Navigate to the path like : \\192.168.2.50\ftp
Delete \ftp, the address in folder explorer should be \\192.168.2.50, find the folder you want, right click and map network drive.
You can try it with this address ftp:\\192.168.2.50, it will pop up a window. Input you usename and password, then you can check the files.
Test Result
Sample code
[HttpGet("directorio")]
public IActionResult GetDirectoryContents()
{
string networkPath = #"ftp:\\192.168.2.50";
string userName = #"Administrator";
string password = "Yy16";
#region FtpWebRequest
var networkCredential = new NetworkCredential(userName, password);
var uri = new Uri(networkPath);
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = networkCredential;
request.Method = WebRequestMethods.Ftp.ListDirectory;
try
{
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
catch (WebException ex)
{
Console.WriteLine("Access to the path '" + networkPath + "' is denied. Error message: " + ex.Message);
}
#endregion
return Ok();
}
In a ASP.Net Core 6 Blazor server application, is it possible to return a pdf file from a razor page? I'm using wkhtmltopdf to generate the pdf when requested by a user. The pdf is generated and returned as a byte array. Currently the byte array goes into a MemoryStream and a Javascript method is called which will allow the user to save the pdf to their computer. I would rather the pdf open in the web browser. Is this possible?
Button click on the razor page calls the following method.
async Task ViewPdf() {
// create the pdf writer object
Writer = WritePDF.CreateInstance();
// call wkhtmltopdf to generate the pdf
var pdf = Writer.Value.Execute();
var ms = new MemoryStream(pdf);
using var streamRef = new DotNetStreamReference(stream: ms);
await JSRuntime.InvokeVoidAsync("myApp.downloadFile", "mypdf.pdf", streamRef);
}
I've Blazor Application (Blazor Server) with side menu. When you click on one of these menus, you will open PDF file based on specific privilege (when clicks on href).
My question :- what if someone changes the URL manually and replace it by the file URL, how I can get this URL or prevent unauthorized user from downloading this file ??
It's better to create a controller to download file, so you can control the download before it starts.
Something like:
My File
In this case the FilesController will have a Download method and inside this method you can check the authorization process.
public FileResult DownloadFile()
{
// logic to allow/disallow users from downloading
byte[] fileBytes = System.IO.File.ReadAllBytes(#"pathtofile"); // or any other source
string fileName = "nameWithExtension";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
While link to this method in controller can be presented to end users
I need to call a web api that returns a pdf. I want to display that pdf when the user clicks a download button. Here's my code. Nothing seems to happen when the button is clicked although through debug I can see the successful download of the file from the api and writing to local disk. I'd prefer to send the file straight to the browser instead of downloading. Here is what I've tried.
On the page
button #onclick="#(e => Download_doc())">Download
In my .cs
var request = new HttpRequestMessage(HttpMethod.Get, "URL");
HttpResponseMessage resp = await client.SendAsync(request);
var file = System.IO.File.Create("download.pdf");
var content = await resp.Content.ReadAsStreamAsync();
await content.CopyToAsync(file);
Console.Write(file);
return file;
Any suggestions would be appreciated.
Team,
I have a blazor web assembly app, which upload the file and process it later. However , I would like to know the base path of the file from where it it picked in the machine.
My code goes as follows . Does anyone has idea to get the file path such as "C:\myfile.txt".
With the File object, I cannot achieve the full path, I can access only its memory stream.
<h1>FILE UPLAOD </h1>
<InputFile OnChange="HandleSelection" ></InputFile>
#code
{
string status;
async Task HandleSelection(IFileListEntry[] files)
{
var file = files.FirstOrDefault();
if (file != null)
{
// Just load into .NET memory to show it can be done
// Alternatively it could be saved to disk, or parsed in memory, or similar
var ms = new MemoryStream();
await file.Data.CopyToAsync(ms);
Console.WriteLine(ms);
status = $"Finished loading {file.Size} bytes from {file.Name}";
var content = new MultipartFormDataContent
{
{ new ByteArrayContent(ms.GetBuffer()),"\"upload\"", file.Name}
};
await client.PostAsync("upload", content);
}
}
}
Even if you get the fullpath (C:\myfile.txt") file won't load
by default, all browser has a security mechanism that any local disk file won't be loaded into a website until you disable that security for your website