How download binary files with pentaho? - pentaho

The Http Client step of pentaho only allows download of text files.
How can I download binary files with Pentaho?

Add a Modified Java Script Value with the following code
var fileURL = "https://hp.imguol.com.br/c/home/b1/2018/05/26/mohamed-salah-chora-apos-se-machucar-em-lance-com-sergio-ramos-1527363329053_300x300.jpg";
var url = java.net.URL(fileURL);
var httpConn = url.openConnection();
// opens input stream from the HTTP connection
var inputStream = httpConn.getInputStream();
var saveFilePath = "d:/myfile10.jpg";
var bis = java.io.BufferedInputStream(inputStream);
var bos = java.io.BufferedOutputStream(java.io.FileOutputStream(java.io.File(saveFilePath)));
var inByte;
while((inByte = bis.read()) != -1) {
bos.write(inByte);
}
bis.close();
bos.close();

Related

Send Image to the API using image path that stored in local database

I am trying to send image to the API, I have multiple local storage image path which is stored in local
database sqlite and I have to upload these image on one click. in the
local database. replace _mediaFile = await CrossMedia.Current.TakePhotoAsync
with _mediaFile = Image_Path_From_Database
The datatobesync it contain multiple local storage image path from local dat
conn = await DependencyService.Get().GetConnection();
List datatobesync = new List();
datatobesync = (from c in conn.Table<CDCInfo>()
where c.SyncStatus == 0 && c.UserName == Settings.Username
select new CDCInfo
{
PhotoPath = c.PhotoPath,
}
I have a string image path but I am not sure about how
to use this
Using MediaPlugin for opening a gallery or camera. After selecting the
picture I set that picture to _mediafile and added to the content and send it to the API.
Detail Here
these lines of code gets image from the gallery or camra (using MediaPlugin) in a
mediaFile and send it to the API
private MediaFile _mediaFile;
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.
StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
AllowCropping = true,
PhotoSize = PhotoSize.Medium
});
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),"\"Files\"", $"\"{_mediaFile.path}\"");
var httpClnt = new HttpClient();
result = await httpClnt.PostAsync(Urle, content);
How can I add image to the content using database path and send it to the API without using MediaPlugin.
I am trying to do something like
foreach (var item in datatobesync)
{
try
{
HttpClient client = new HttpClient();
MultipartFormDataContent content = new
MultipartFormDataContent();
content.Add(new
StreamContent("Here I want to user item.PhotoPath Image path from the database".GetStream()),"\"Files\"",
$"\"{item.PhotoPath}\"");
var httpClnt = new HttpClient();
result = await httpClnt.PostAsync(Urle, content);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I dont know how to use image path at this line
content.Add(new StreamContent(How_to_User_ImagePath_Her.GetStream()),"\"Files\"",
$"\"{item.PhotoPath}\"");
How to get GetStram() of image path

CloudBlockBlob DownloadTextAsync Behavior Difference

I am using an azure function with event grid trigger and CloudBlockBlob as input binding. The content is getting downloaded from CloudBlockBob using DownloadTextAsync(AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
If the file being downloaded above is being generated using XmlDocument, DownloadTextAsync returns gibberish. However, if the file is generated by using FileStream, it works fine. PFB the implementations of generating the file-
Using XmlDocument
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(typeof(List<ContractName>), new XmlRootAttribute("RootAttributeName"));
serializer.Serialize(stringwriter, contractData);
var xmlString = stringwriter.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
doc.PreserveWhitespace = true;
doc.Save(fileName);
Using FileStream
var serializer = new XmlSerializer(typeof(List<ContractName>), new XmlRootAttribute("RootAttributeName"));
var file = new FileStream(fileName, FileMode.OpenOrCreate);
serializer.Serialize(file, contractData);
file.Close();
Code being used to download the content-
Using DownloadTextAsync
private static async System.Threading.Tasks.Task<string> DownloadContentAsync_DownloadTextAsync(string storageAccountConnectionString, string containerName, string blobName)
{
CloudBlobContainer container = GetContainer(storageAccountConnectionString, containerName);
ICloudBlob blob = await container.GetBlobReferenceFromServerAsync(blobName);
// Download the blob content
string xmlBlobContent =
await (blob as CloudBlockBlob).DownloadTextAsync(
null,
new BlobRequestOptions { LocationMode = LocationMode.PrimaryThenSecondary },
new OperationContext());
return xmlBlobContent;
}
Using DownloadToStreamAsync
private static async System.Threading.Tasks.Task<string> DownloadContentAsync_DownloadToStreamAsync(string storageAccountConnectionString, string containerName, string blobName)
{
CloudBlobContainer container = GetContainer(storageAccountConnectionString, containerName);
ICloudBlob blob = await container.GetBlobReferenceFromServerAsync(blobName);
// Download the blob content
MemoryStream resultStream = new MemoryStream();
await (blob as CloudBlockBlob).DownloadToStreamAsync(
resultStream,
null,
new BlobRequestOptions { LocationMode = LocationMode.PrimaryThenSecondary },
new OperationContext());
string xmlBlobContent = System.Text.Encoding.UTF8.GetString(resultStream.ToArray());
return xmlBlobContent;
}
Why there is a difference in response from DownloadTextAsync.
Updated 0713:
Figured it out. The root cause is that when you're using XmlDocument to generate the xml file, the encoding is utf-16. But for FileStream, it generates the xml file with encoding utf-8.
So, the solution is that, when using XmlDocument, we can specify the encoding to utf-8(no code change for FileStream). Sample code as below:
Generate xml file using XmlDocument:
//2. Using XMLDoc
serializer.Serialize(stringwriter, contractData);
var xmlString = stringwriter.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
doc.PreserveWhitespace = true;
string fileName = String.Format(#"C:\TestBlobDownloadContent\UsingXMLDoc" + count + ".xml");
//encoding as utf-8
using (TextWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
{
doc.Save(sw);
}
When read the xml file from blob storage via DownloadTextAsync() method, no need to specify the encoding option, like below:
// Download the blob content
string xmlBlobContent =
await (blob as CloudBlockBlob).DownloadTextAsync(
null,
new BlobRequestOptions { LocationMode = LocationMode.PrimaryThenSecondary },
new OperationContext());
Original answer:
This is due to the encode/decode issue.
Solution:
In the DownloadTextAsync() method, add parameter System.Text.Encoding.Unicode. Like below:
string xmlBlobContent =
await (blob as CloudBlockBlob).DownloadTextAsync(
System.Text.Encoding.Unicode,
null,
new BlobRequestOptions { LocationMode = LocationMode.PrimaryThenSecondary },
new OperationContext());
The test result:

Unable to download file using phantomjs

When I click on a button in chrome, it downloads a jpg file.
But when I am using phantomjs it doesn't download the jpg nor gives any error.
example: https://deepak5j.github.io/HelloProjectPage/download.html
How to download file with phantomjs ?
Using casperjs (based on phantomjs) :
var casper = require("casper").create();
casper.start('https://deepak5j.github.io/HelloProjectPage/download.html', function() {
var url = 'https://raw.githubusercontent.com/Deepak5j/WebImages/master/Tiles/';
this.download(url, 'sun_tile.jpg');
});
casper.run(function() {
this.exit();
});
Button click with phantom will not download file but after click you can get file as input stream. So you can save input stream to file. Code below will give an idea:
driver.findElement(By.className("Download")).click();
List<LogEntry> harLogEntries = driver.manage().logs().get("har").getAll();
LogEntry lastLogEntry = harLogEntries.get(harLogEntries.size() - 1);
String lastRequestUrl = getRequestUrlFromHarLogEntry(lastLogEntry);
DefaultHttpClient client;
HttpResponse response;
HttpGet get = new HttpGet(lastRequestUrl);
response = client.execute(get);
InputStream dataStream = response.getEntity().getContent();
I will share getRequestUrlFromHarLogEntry below:
private String getRequestUrlFromHarLogEntry(LogEntry logEntry)
throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> message = objectMapper.readValue(logEntry.getMessage(), Map.class);
Map<String, Object> log = (Map<String, Object>) message.get("log");
List<Object> entries = (List<Object>) log.get("entries");
Map<String, Object> lastEntry = (Map<String, Object>) entries.get(entries.size() - 1);
Map<String, Object> request = (Map<String, Object>) lastEntry.get("request");
String url = (String) request.get("url");
return url;
}
Hope this helps..

UWP : Links does not work in PDF after generating a PDF with ReportWriter (SyncFusion)

I'm using a UWP application to generate a .pdf file from an .rdlc template file with Syncfusion components. I can generate the pdf from the rdlc template file but all the links in the pdf (text or image) does not work. The links work if I generate an html file but not in pdf file. Here is the code :
var pdfFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(String.Concat("Order-", this.Model.SaleReference, ".pdf"), CreationCollisionOption.ReplaceExisting);
using (var stream = await pdfFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outstream = stream.AsStreamForWrite())
{
var assembly = typeof(FinalizeViewModel).GetTypeInfo().Assembly;
var reportStream = assembly.GetManifestResourceStream("UWP.OrderModule.Reports.Test.rdlc");
var writer = new ReportWriter(reportStream)
{
ExportMode = Syncfusion.ReportWriter.ExportMode.Local
};
writer.Save(outstream, WriterFormat.PDF);
outstream.Dispose();
}
}
await Windows.System.Launcher.LaunchFileAsync(pdfFile);
I also try with this code but the result is the same :
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(String.Concat("Order-", this.Model.SaleReference, ".pdf"), CreationCollisionOption.ReplaceExisting);
using (var stream = await WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file))
{
var assembly = typeof(FinalizeViewModel).GetTypeInfo().Assembly;
var reportStream = assembly.GetManifestResourceStream("UWP.OrderModule.Reports.Test.rdlc");
var writer = new ReportWriter(reportStream)
{
ReportProcessingMode = Syncfusion.ReportWriter.ProcessingMode.Local,
ExportMode = Syncfusion.ReportWriter.ExportMode.Local
};
writer.Save(stream, WriterFormat.PDF);
}
await Windows.System.Launcher.LaunchFileAsync(pdfFile);
What is wrong ? Is there a problem in the ReportWriter class (Syncfusion.RdllO.RdllOExportEngine class) ?
Thanks for your help.
Currently Syncfusion ReportWriter don't have support for exporting report as PDF with hyperlink option. We will consider to include this feature in any of our upcoming release.

download doc file in MVC

I have applicaiton which is the combination of MVC 4 + Web Api + SQL server.
I am trying to download the doc file to MVC but i have tried the below step.
I have Web API where i have written the below code. when i send the rowid it has the value stored in the DB as varbinary. file format can be any thing like .doc,pdf etc ... but however I am looking for the first doc or PDF file format.
When I call the Web api it will create the PDF file and download it , but the file is completely corrupted.
[ResponseType(typeof(MandateExceptionDO))]
[HttpGet]
[Route("api/DealingMandate/GetExceptionDoc/{rowId}")]
public HttpResponseMessage GetExceptionDoc(int rowId)
{
IDealingMandates repository = new DealingMandatesRepository();
List<MandateExceptionDO> mandateexceptiondoc =new List<MandateExceptionDO>();
mandateexceptiondoc = repository.GetExceptionDoc(rowId);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
//response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
//byte[] fileBytes = System.IO.File.ReadAllBytes(mandateexceptiondoc[0].Content);
response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "testing.pdf";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//return Ok(mandateexceptiondoc);
return response;
}
I am able to fix this issue on the web api i made the byte as string as show below
String doc = Convert.ToBase64String(customermandate.Content);
and for the MVC side i converted back to byte from the string
var doc = restClient.Execute(request);
var response = doc.Content.Substring(1, doc.Content.Length - 2).Replace(#"\/", "/");
byte[] docBytes = Convert.FromBase64String(response);
if (doc.Content != null && doc.Content.Length > 0 && !string.IsNullOrEmpty(doc.Content))
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
Response.BinaryWrite(docBytes);
Response.End();
}