How to download file in Xamarin.Forms that are in Base64 format? - xamarin

I am creating an application that download and upload attachment from the Xamarin.Forms application.
I am using .Net standard project and try to use with the "CrossDownloadManager" Nuget but it is not compatible and other reference for download file are using url but in this situation I have base64byte string.
I also try with This reference but it doesn't work for me.
Application works on Android and iOS.
I attached sample response Here:
Can anyone look into this and suggest me what should I have to do in that?

download a file given a url
var client = new HttpClient();
var data = await client.GetStringAsync(url);
if data is Base64, decode it
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var decoded = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
save decoded string
File.WriteAllText(filepath, decoded);

Related

How to upload image to Google Drive using MIT APP Inventor II

I'm trying to upload selected image by MIT App Inventor II. What I did is to download an extension which encodes image to base64 coding, and then call Web1 to address Google App Script. Actually, what I did is just follow an YouTube channel, but somehow it does not work.
function doPost(e) {
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data,e.parameters.mimetype,e.parameters.filename);
var folderid = Utilities.base64Decode(e.parameters.folderid);
DriveApp.getFileById(folderid).createFile(blob);
return ContentService.createTextOutput("Your File Successfuly Uploaded");
}
Combining with Taifun's Extension. These blocks are confirmed to be able to do as below.
Display the ImagePicker selected image on the Image component
Encode the ImagePicker selected image to a Base64 string.
Send the string to URL (Google App Script)

Android Xamarin.Auth 1.2.2 How to Update Uploaded Cloud Storage File LastModifiedDate?

With Xamarin.Forms, I am using OneDrive SDK on UWP to access OneDrive and it is working good. I am uploading/downloading small data files and I use the following code to change a file's LastModifiedDate:
Item itemUpdate1 = new Item();
itemUpdate1.FileSystemInfo = new Microsoft.OneDrive.Sdk.FileSystemInfo {LastModifiedDateTime = lastModifiedDateTime };
await oneDriveClient1.Drive.Items[item1.Id].Request().UpdateAsync(itemUpdate1);
On Android, I use Xamarin.Auth to access OneDrive and I cannot figure out how to update a file's LastModifiedDate. I am using the following code to sign in and upload the file:
var auth = new OAuth2Authenticator(clientId: clientId, scope: storageScopes1, authorizeUrl: new System.Uri("https://login.live.com/oauth20_authorize.srf"),
redirectUrl: new System.Uri("https://login.live.com/oauth20_desktop.srf"));
System.Uri dataFileUri = new System.Uri("https://api.onedrive.com/v1.0/drive/special/approot:/" + dataFileName1 + ":/content");
var requestUpload = new OAuth2Request("PUT", dataFileUri, null, account);
I would like to know if OAuth2Request can be used to update the file's LastModifiedDate or if there is another way to do it?
Thanks for your help.
Short answer is no. Xamarin.Auth is only to handle the Authentication between your App and the OneDrive Rest Api.
If you want to modify any of the properties of a file in OneDrive you will need to either use a OneDrive SDK for Android as you do in the UWP project or do these modification using the Rest API directly as you did to upload the file.
UPDATE 1
System.Uri dataFileUri = new System.Uri("https://api.onedrive.com/v1.0/drive/special/approot:/" + dataFileName1 + ":/content");
var requestUpload = new OAuth2Request("PUT", dataFileUri, null, account);
As you did in the code above, if you can get the endpoint for the OneDrive REST API to modify the file's properties (like LastModifiedDate) you could be able do it with OAuth2Request.
Go to the OneDrive Dev Portal and try to get that information from the documentation.

(UWP) WebClient and downloading data from URL in

How do you download data from an url in UWP? I'm used to using WebClient's DownloadData-method, but it can't be used anymore.
.NET for UWP does not have the WebClient class.
But you have several alternative ways to download the data from URL in UWP.
For example:
var request = WebRequest.CreateHttp("http://www.bing.com");
var donnetClient = new System.Net.Http.HttpClient();
var winrtClient = new Windows.Web.Http.HttpClient();
If you want to download the data at background, you can use the BackgroundDownloader class

Opening a PDF file in Windows Phone

I'm developing an app for Windows Phone 7 and I'm using a Phonegap template for it.
Everything looks perfect, but now I’m stuck trying to open a PDF file in the browser.
I tried the following but that doesn’t work because the url of the PDF exceeds the 2048 character limit (it’s a data url). This code runs after the deviceReady event was fired.
var ref = window.open('http://www.google.com', '_blank', 'location=no');
ref.addEventListener('loadstart', function () { alert(event.url); });
Now, I'm trying to save the PDF file to storage and then I'm trying to have it opened by the browser, but the browser doesn't show anything. I'm editing the InAppBrowser.cs code from cordovalib and I added the following lines before calling browser.Navigate(loc);
private void ShowInAppBrowser(string url)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
FileStream stream = store.OpenFile("test.pdf", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
var myvar = Base64Decode("the big data url");
writer.Write(myvar);
writer.Close();
if (store.FileExists("test.pdf")) // Check if file exists
{
Uri loc = new Uri("test.pdf", UriKind.Relative);
...
}
}
This code is returning the following error:
Log:"Error in error callback: InAppBrowser1921408518 = TypeError: Unable to get value of the property 'url': object is null or undefined"
I don’t wanna use ComponentOne.
Any help would be greatly appreciated!
You cannot open pdf files from the isolated storage in the default reader for PDF files. If the file is online e.g. it has a URI for it, you can use WebBrowserTask to open it since that will download and open the file in Adobe Reader.
On Windows Phone 8 you actually can open your own file in default file reader for that extension, but I am not sure how that will help you since you target PhoneGap and Windows Phone 7.
Toni is correct. You could go and try to build your own viewer (which would be the same thing as using C1, but with more time involved). I worked on a port of iTextSharp and PDFSharp for WP7, but neither of which are PDF Viewers. They are good for creating PDFs and parsing them some (but to render them there is more work involved). This has been a personal quest of mine, but honestly the best I have gotten is to be able to extract some images from the PDF (and none of the text)
try this
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assets = await installedLocation.GetFolderAsync("Assets");
var pdf = await assets.GetFileAsync("metro.pdf");
Windows.System.Launcher.LaunchFileAsync(pdf);
This worked correctly on my Device.

Unable to access Asset files in Metro app

I am trying to read a text file, which is shipped as an asset in a Metro app. I am receiving an access denied error, if specifying the file path as "ms-appx:///Assets/file.txt". Clearly I need to set some capability to access installation location folder. I tried enabling all capabilities in manifest designer, but still the same error. Please suggest.
Try this:
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/file.txt"));
Stream stream = await file.OpenStreamForReadAsync();
StreamReader sr = new StreamReader(stream);
Is that exactly the reference you are using? Try
ms-appx:///Assets/file.txt

Resources