Save file into external storage and open it - xamarin.forms

I have a pdf file that is currently saved in internal storage.
I would like to either save this file to external storage and open it or open it directly from internal storage.
My code:
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HelloWorld.pdf");
document.Save(fileName);
Thanks for help

One way is to use Xamarin.Essentials.Launcher. It enables an application to open a URI by the system. But it would not open a pdf file. You could open it by Word if your device have installed Microsoft Word app.
Refer to the MS docs for code: https://learn.microsoft.com/en-us/xamarin/essentials/launcher?tabs=ios#files
A easy way is to use a webview to display PDF.
Xaml:
<WebView WidthRequest="300" HeightRequest="300" x:Name="MyWebview"></WebView>
Code behind:
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(documentsPath, "a.pdf");
MyWebview.Source = filePath;

Related

Save file opened with navigateToURL

I'm using navigateToURL to open a PDF file generated by Jasper and it's working well. Now I was asked to send an email with the PDF file as an attachment. We are using Velocity to send emails in our application but, to send attachments, we have to have the file saved somewhere and have the info of the path and filename.
Is it possible to save to file opened by navigateToURL?
I'm using Flex SDK 4.9.1.
Thank you.
Are you using JasperExportManager.exportReportToPdfStream() method to show the PDF?
If so, try to use JasperExportManager.exportReportToPdfFile().
Then you can save the PDF at server.
JasperPrint jasperprint = JasperFillManager.fillReport(jasper, paramMap, con);
// Streaming pdf now?
response.setContentType("application/pdf");
JasperExportManager.exportReportToPdfStream(jasperprint, response.getOutputStream());
// Save the PDF somewhere.
JasperExportManager.exportReportToPdfFile(jasperprint, path);

GoogleDrive downloadURL is not populating

I am trying to integrate Google-docs UI with ASP.NET Web application.
In this files are stored on our file storage and We want to open Google Docs Editor to edit the word/excel/PPT files.
As a part of POC for this requirement, I am doing below:
Uploading file into Google Drive using below code:
Dim objInsert As Google.Apis.Drive.v2.FilesResource.InsertMediaUpload = objDriveService.Files.Insert(objFile, objStream, sMimeType)
objInsert.Convert = True
objInsert.Upload()
Getting response after upload and using objFile.AlternateLink
property to open the Editor to edit file.
I am facing the issue to download the file when I am using
objInsert.Convert = True. when I am making the call to File object
my Download URL (objFile.DownloadUrl) is not returning it is set
with Null. If I don't use the objInsert.Convert = True then I
am getting Download URL but problem is then AlternateLink open the
file in View mode with various option, I want the URL to open it
directly in editor.
Please suggest what am I missing here.
UPDATE: this was already asked here: DownloadUrl missing from metadata for file on Google Drive API
I think it is the expected behaviour. You can't directly download a Google file type, you have to export it to some compatible format.

How to create a file on server and save it on server

I m creating a pdf file on the server using iTextSharp.
It is working fine prompting me to save everything is fine.
I have 2 scenario
1) I want to save this file on a location on server
2) I want to save this file in database
My Last line after finishing the code is
document.Close();
So can anyone guide me how can I do it
The following method will create a new file on the disk with a paragraph in it. You could easily modify it to your needs.
Before this will work you must ensue the directory you wish to save to has the correct permissions set. Whatever user account the Application Pool for the website is using will need write access to the folder. Set this in Windows Explorer, Right Click > Properties > Security Tab. This is well documented on the internet.
You can store the filename in the database in many different ways and then link to the file from your UI. I suggest starting with a simple ADO.Net tutorial
protected void CreateDoc(string filename)
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World!"));
document.Close();
}

How to get file path using FileUpload to be read by FileStream?

I have a Method that open excel file and read it through exceldatareaderClass that i have downloaded in codeplex by using filestream.
Currently I just declared the exact directory where the filestream open an excel file.And it works fine.
Stream stream = new FileStream("C:\\" + FileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Now i need to read the excel file wherever location the user place it like on windows forms fileupload.FileStream needs the exact location where the file is located. How to do this.?
Example: Sample.xls is located on My Documents
the file path should be like : C:\Documents and Settings\user\My Documents\
string openpath ="" ;//filepath
Stream stream = new FileStream(openpath+ FileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Thanks in Regards
The FileUpload property PostedFile.InputStream already represents the stream that you create in your example.
Note that the FileName property stores the file name on the client, not the file name on the server.
You cannot expect that the uploaded file is present as a physical file on the server, just use the InputStream to read the uploaded data.

ASP .NET invalid path

I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
using System.IO;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Path.Combine(#"X:\Your\Own\Upload\Folder", filename);
FileUpload1.PostedFile.SaveAs(filepath);
// Now use `filepath` as your data source.
IIS might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.

Resources