Load XML file (with XSL file) onto UIWebView - ios

I added TestXML.xml together with simple.xls file to my Resources folder. Now I want to display the xml styled according to the xsl file.
string fileName = "TestXML.xml";
string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
NSUrl url = new NSUrl (localDocUrl, false);
NSData xml = NSData.FromUrl (url);
string contentDirectoryPath = NSBundle.MainBundle.BundlePath;
NSUrl baseURL = new NSUrl (contentDirectoryPath, true);
webView.LoadData (xml, "text/xml", "UTF-8", baseURL);
webView.ScalesPageToFit = false;
If I execute the code above I get an empty page. How do I load the styled XML file onto my UIWebView?
EDIT
Now I tried to load a web ressource:
string fileName = "http://www.w3schools.com/xml/simplexsl.xml";
NSUrl url = new NSUrl(fileName);
NSMutableUrlRequest request = new NSMutableUrlRequest (url, NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, 10);
webView.LoadRequest(request);
Here the xml file is correctly loaded with the xsl file. But how could I use a string or a local file for displaying it in the UIWebView?

Related

How to open a downloaded file

I am writing an application for Xamarin.iOS, but an answer in native iOS would suffice. After I download a file (image or pdf) I want to open it:
public void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
// how to open location?
}
I tried UIApplication.SharedApplication.OpenUrl(location); but nothing happens.
I'm not sure whether you mind using WKWebView to load the local file, and if you store the file in Library/Caches/ of Application directories, you could use follow ways to load the downloaded file.
Get the cache path:
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
cache = Path.Combine(documents, "..", "Library", "Caches");
Console.WriteLine("path is : "+cache);
Load a tree.png in WKWebView:
webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
View.AddSubview(webView);
NSUrl nSUrl = new NSUrl("file://" + cache + "/tree.png");
webView.LoadFileUrl(nSUrl, nSUrl);
Here is the output:
2020-10-28 10:27:47.303745+0800 IOSSplashScreen[4370:73093] path is : /Users/xxx/Library/Developer/CoreSimulator/Devices/C30029F0-379A-4255-B38B-27E2BAAC8CC1/data/Containers/Data/Application/5C7D7CA5-7394-414A-897F-8F1A278838A8/Documents/../Library/Caches
And effect:

RestBuilder Plugin. How can I upload a file without creating a file?

Currently, I can upload files(exist) with Grails's RestBuilder.
However, I want to upload a file without creating a file .
I want to create binary data (= Text File) in a program and send it directly
Is it possible?
RestBuilder rest = new RestBuilder()
RestResponse resp = rest.post(url){
contentType("multipart/form-data")
setProperty("dataFile",[filePath])// <- it can
setProperty("dataFile",[ byte[] or inputStream() or String ? ])// <- Is it possible?
}
'''
I'm sure you figured this out already, but you can just use a String reference or a byte[] just as you can use File instances for the multipart request using RestBuilder. It should 'just work' e.g.
RestBuilder rest = new RestBuilder()
RestResponse response = rest.post(url) {
contentType 'multipart/form-data'
stringPart = 'hello' // String
bytePart = '68656c6c6f'.decode64() // byte[]
filePart = new File('/path/to/file.jpg') // File
}

Uri is not supported when saving pdf in server folder with nreco pdf generator

I have the following code:
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.PdfToolPath = "~/files/";
htmlToPdf.GeneratePdf(template);
Which throws the following error:
Uri is not supported when saving pdf in server folder with nreco pdf generator.
You will need to set a regular path to your file system like e.g. "C:\temp\myfolder\". Or use a . instead of ~ and backslashes:
htmlToPdf.PdfToolPath = ".\\files\\";
If NReco is able to deliver you an byte-array or a stream you should prefer this instead of a file and return it directly.
UPDATE:
After takeing a look into the documentation of NReco all you need to do is following:
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.PdfToolPath = "<CORRECT_PATH_FOR_TOOL>";
var output = htmlToPdf.GeneratePdf(template);
System.IO.File.WriteAllBytes("<OUTPUT_PATH>", output);
This should create your pdf in the OUTPUT_PATH.
#OlaFW thanx for your effort.
I got my answer.
var pdfBytes = htmlToPdf.GeneratePdf(template);
string filePath = "/files/Myfile.pdf";
string Url = System.Web.Hosting.HostingEnvironment.MapPath(filePath);
System.IO.File.WriteAllBytes(Url, pdfBytes);

FSharp.Data - Save the downloaded file using the website responded name?

I have the following function to download file.
let download url folder name =
use out = File.Create(Path.Combine(folder, name))
Http.RequestStream(url, cookieContainer = cc, headers = headers)
.ResponseStream.CopyTo(out)
However, I need another download function which just use the name from the website. How to get the name?
let download url folder =
let name = .... // get from the default name set by the website?
use out = File.Create(Path.Combine(folder, name))
Http.RequestStream(url, cookieContainer = cc, headers = headers)
.ResponseStream.CopyTo(out)

C# Open Url and download a file, the last part of the file name changes

I want to download a file from an URL, the file name is updated frequently
For E.g.: filename_Date.zip where date changes.
Below is the Query I used
WebClient webClient = new WebClient();
webClient.DownloadFile("http://nppes.viva-it.com/NPPES_Deactivated_NPI_Report_081214.zip", #"C:\Users\gnanasem\Documents\NPIMatcher\NPI.zip");
Landing Page of the URL: http://nppes.viva-it.com/NPI_Files.html
Here you can see multiple files and I want to download the first one.
One way of doing this is to:
Get the HTML of the webpage
Find the URLs on the webpage using RegEx
Find the first URL containing the relevant text, in your case "Deactivated"
Download the file
I got it working like this:
using (var webClient = new WebClient())
{
var websiteHtml = webClient.DownloadString("http://nppes.viva-it.com/NPI_Files.html");
var urlPattern = "href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))";
Match match = Regex.Match(websiteHtml, urlPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1));
var urlToDownload = string.Empty;
while (match.Success)
{
var urlFound = match.Groups[1].Value;
if (urlFound.ToLower().Contains("deactivated"))
{
urlToDownload = urlFound;
break;
}
match = match.NextMatch();
}
webClient.DownloadFile(urlToDownload, #"C:\Users\gnanasem\Documents\NPIMatcher\NPI.zip");
}

Resources