I have a OneDrive shared link for an excel file with some data. And I need to read this excel file through my C# ASP.NET MVC web application without downloading it to local PC. But I'm new to this 'OneDrive API'. So I have a serious challenge with this. I'm familiar with local file read as in,
EX:
var package = new ExcelPackage(new System.IO.FileInfo(PATH));
I'd really appreciate if anyone can guide me or provide a sample code to do this. I just need to access the ExcelPackage of the .xlsx file. Thank you in advance.
PS : I have tried some options like 'WebRequest', 'Webclient', etc. :(
You could try something like HttpClient.GetStreamAsync to open a download stream for the remote file and then pass the returned Stream to the ExcelPackage constructor (documentation states it has a constructor that accepts an instance of a Stream).
Hope it helps!
Related
I am making an app that allows you to open and edit a pdf file on tablets. Because i usually work with .NET, i decided to write it in .NET MAUI. That way i also have access to windows tablets.
It uses Itext as its main library to read and edit the pdf's.
I have an external shared fileserver that anyone can access when they are coneected to the WIFI.
I'd like to access that fileserver when i connect from my android tablet using Itext pdfreader.
How do I achieve this correctly?
Am i missing a library or a package which would allow to me to access that file?
Are there options i haven't discovered yet?
This works on windows tablets:
string dest "\\\\Path\\to\\File\\";
string file = "\\\\Path\\to\\File\\file.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfReader(file), new PdfWriter(dest));
I have tried :
string file = Environment.GetFolderPath(Environment.SpecialFolder.Windows)+ "\\Path\to\File\file.pdf";
string file = "\\\\Path\\to\\File\\file.pdf";
All of them result in file not found
Among the getfolderpath options ive tried a dozen, none of them seem to work.
thank you for your time
So i ended up solving this by transforming the document into a base64 string and sending it through an api that i had.
i used the classic httprequest aproach that you can look up and copy anywhere.
I have been trying to upload to a OneDrive account and I am hopelessly stuck not being able to upload neither less or greater than 4MB files. I have no issues accessing the drive at all, since I have working functions that create a folder, rename files/folders, and a delete files/folders.
https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=csharp
This documentation on Microsoft Graph API is very friendly to HTTP code, and I believe I am able to fairly "translate" the documentation to C#, but still fail to grab a file and upload to OneDrive. Some places online seem to be using byte arrays? Which I am completely unfamiliar with since my primary language is C++ and we just use ifstream/ofstream. Anyways, here is the portion of code in specific (I hope this is enough):
var item = await _client.Users[userID].Drive.Items[FolderID]//"01YZM7SMVOQ7YVNBXPZFFKNQAU5OB3XA3K"].Content
.ItemWithPath("LessThan4MB.txt")//"D:\\LessThan4MB.txt")
.CreateUploadSession()
.Request()
.PostAsync();
Console.WriteLine("done printing");
As it stands, it uploads a temporary file that has a tilde "~" in the OneDrive (like as if I was only able to open but not import any data from the file onto it). If I swap the name of the file so it includes the file location it throws an error:
Message: Found a function 'microsoft.graph.createUploadSession' on an open property. Functions on open properties are not supported.
Try this approach with memory stream and PutAsync<DriveItem> request:
string path = "D:\\LessThan4MB.txt";
byte[] data = System.IO.File.ReadAllBytes(path);
using (Stream stream = new MemoryStream(data))
{
var item = await _client.Me.Drive.Items[FolderID]
.ItemWithPath("LessThan4MB.txt")
.Content
.Request()
.PutAsync<DriveItem>(stream);
}
I am assuming you have already granted Microsoft Graph Files.ReadWrite.All permission. Check your API permission. I tested this code snippet with pretty old Microsoft.Graph library version 1.21.0. Hopefully it will work for you too.
Have a problem to read data from CloudFile. I'm using Azure File Storage, .net core 2.1.
Try to read data from the file that was uploaded to File storage.
Get a reference to file, but don't understand what to do next. Try to use different solutions, but they not work
cloudFile = directoryReference.GetFileReference(fileName);
Can you please help how to work with it to read all data(text, etc.)?
Will have formats as .doc, .pdf, etc.
Thanks!
You could download the text from your doc (this is from the example in the link below):
string text = cloudFile.DownloadTextAsync().Result;
Take a look at this. It explains how to use the storage API specifically the file storage API
https://learn.microsoft.com/en-us/azure/storage/files/storage-dotnet-how-to-use-files
I am able to upload file(s) from system to server using uploadify control(www.uploadify.com). Everything is working fine but thing is that I am not getting how to check the file existing or not in the server. I need help from you that how to check existing file(s) using java script or C# before uploading file(s) to server. I have code for checkexisting in php but I want in C# or java script so kindly please kindly do needful.
Thanks
PBN
Not sure what you are asking, but checking if a file exists with C# could be done with:
string path = #"C:\yaddayadda\file.txt";
bool fileExists = File.Exists(path);
MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
But perhaps you were asking something else? In that case please clarify.
i have a URL and when i load it up in a browser it recognizes it as a csv file and pops up excel "do you want to open". I want to do this programatically so i can have a winforms app use that url and parse the csv file directly.
what is the quickest way to do this?
EDIT: i tried using WebClient and i am getting the folowing error:
"The remote server returned an error: (500) Internal Server Error."
I don't see why something like this wouldn't work (in C#):
// Download the file to a specified path. Using the WebClient class we can download
// files directly from a provided url, like in this case.
System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);
Where the url is your site with the csv file and the csvPath is where you want the actual file to go.
If you have a WinForms app, you can use a System.Net.WebClient to read the data as a string.
It will read the entire csv file as a string, but you can write it out or parse it at will.
If you want to just whip something together I would suggest using a scripting language and some bash. Just use wget or something similar to get the file and some scripting language to parse it. You could even use php to parse it once you had the file because I know that php has the following function which is very nice: http://php.net/manual/en/function.fgetcsv.php
I would suggest doing it this way because it is easier, this will certainly let you parse it easily enough though I don't know what you want to do with it from there but the worlds your oyster.
The following code works for me but I am running Open Office. I have not tested it with Excel.
The hacky bit is to rename the local copy of the file to *.xls so that Windows will launch Excel by default, if you leave the file extension as CSV, Windows will launch Notepad by default.
String url = "http://www.example.com/test.csv";
String localfile = "test.xls";
var client = new WebClient();
client.DownloadFile(url, localfile);
System.Diagnostics.Process.Start(localfile);