How to save a byte array to a file from silverlight - wcf

I have a SL 3 application connected to a WCF service. This service retrieves an array of bytes. I'd like to save that array as a pdf file using FileStream. The problem is that when the byte array is retrived, I get an exception when trying to show the SaveFileDialog because that action is initiated by the callback method and not from a user action, it seems.
I'd like to know if there is any workaround for this. I already have the byte array, now I need to save it to a location specified by the user. No matter how...
Any clue?
Thanks in advance.

Are you wiring to the method completed event of your async method call? See this
http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx
Inside your call back method, you can implement the logic for writing to a file - first by opening the dialog, and then by getting the pointer to the file stream as shown below.
try
{
byte[] fileBytes = //your bytes here
SaveFileDialog dialog=new SaveFileDialog();
//Show the dialog
bool? dialogResult = this.dialog.ShowDialog();
if (dialogResult!=true) return;
//Get the file stream
using ( Stream fs = ( Stream )this.dialog.OpenFile() )
{
fs.Write( fileBytes, 0, fileBytes.Length );
fs.Close();
//File successfully saved
}
}
catch ( Exception ex )
{
//inspect ex.Message
}

Related

How to use CSV Dataset Config's variable in Bean Shell Post Processor in Jmeter

In my application I have two scenarios.
1. Create: Here we book a hotel room. After booking application returns a transaction ID.
2. Cancel: We need to pass the transaction Id to the application to cancel booking.
I want to test with jmeter in such a way that after a create call is made, the cancel call of the respective create is called with the generated transaction ID automatically.
So I have created two thread groups. One for create where I am calling create API, saving the transaction Id in a CSV file using Regular Expression Extractor & Bean Shell Post Processor. Another thread is for cancel where I am picking the transaction ID using CSV Dataset Config & calling the cancel API.
Problem is I want to delete that transaction ID from CSV file after calling the cancel API. I think Bean Shell Post Processor will do the job. This is my CSV Data Set Config:
Here is my Bean Shell Post Processor code:
File inputFile = new File("/home/demo/LocalFolder/CSV/result.csv");
File tempFile = new File("/home/demo/LocalFolder/CSV/myTempFile.csv");
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = vars.get("transactionId");
//String lineToRemove = "${transactionId}";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
System.out.println("Completed");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But the transaction ID is not getting deleted from the file. I think that vars.get("transactionId") is not returning anything or returning wrong value. If I hardcode a transation ID then the code works fine. Can anyone help me?
JMeter Variables are local to the current Thread Group only, in order to pass the data between Thread Groups you need to use JMeter Properties (props instead of vars). See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation and usage example.
P.S. Maybe it would be easier to use HTTP Simple Table Server instead?

Read a file from the cache in CEFSharp

I need to navigate to a web site that ultimately contains a .pdf file and I want to save that file locally. I am using CEFSharp to do this. The nature of this site is such that once the .pdf appears in the browser, it cannot be accessed again. For this reason, I was wondering if once you have a .pdf displayed in the browser, is there a way to access the source for that file in the cache?
I have tried implementing IDownloadHandler and that works, but you have to click the save button on the embedded .pdf. I am trying to get around that.
OK, here is how I got it to work. There is a function in CEFSharp that allows you to filter an incoming web response. Consequently, this gives you complete access to the incoming stream. My solution is a little on the dirty side and not particularly efficient, but it works for my situation. If anyone sees a better way, I am open for suggestions. There are two things I have to assume in order for my code to work.
GetResourceResponseFilter is called every time a new page is downloaded.
The PDF is that last thing to be downloaded during the navigation process.
Start with the CEF Minimal Example found here : https://github.com/cefsharp/CefSharp.MinimalExample
I used the WinForms version. Implement the IRequestHandler and IResponseFilter in the form definition as follows:
public partial class BrowserForm : Form, IRequestHandler, IResponseFilter
{
public readonly ChromiumWebBrowser browser;
public BrowserForm(string url)
{
InitializeComponent();
browser = new ChromiumWebBrowser(url)
{
Dock = DockStyle.Fill,
};
toolStripContainer.ContentPanel.Controls.Add(browser);
browser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
browser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
browser.BrowserSettings.WebSecurity = CefState.Disabled;
browser.BrowserSettings.Javascript = CefState.Enabled;
browser.LoadingStateChanged += OnLoadingStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.FrameLoadEnd += browser_FrameLoadEnd;
browser.LifeSpanHandler = this;
browser.RequestHandler = this;
The declaration and the last two lines are the most important for this explanation. I implemented the IRequestHandler using the template found here:
https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/RequestHandler.cs
I changed everything to what it recommends as default except for GetResourceResponseFilter which I implemented as follows:
IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
if (request.Url.EndsWith(".pdf"))
return this;
return null;
}
I then implemented IResponseFilter as follows:
FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
{
BinaryWriter sw;
if (dataIn == null)
{
dataInRead = 0;
dataOutWritten = 0;
return FilterStatus.Done;
}
dataInRead = dataIn.Length;
dataOutWritten = Math.Min(dataInRead, dataOut.Length);
byte[] buffer = new byte[dataOutWritten];
int bytesRead = dataIn.Read(buffer, 0, (int)dataOutWritten);
string s = System.Text.Encoding.UTF8.GetString(buffer);
if (s.StartsWith("%PDF"))
File.Delete(pdfFileName);
sw = new BinaryWriter(File.Open(pdfFileName, FileMode.Append));
sw.Write(buffer);
sw.Close();
dataOut.Write(buffer, 0, bytesRead);
return FilterStatus.Done;
}
bool IResponseFilter.InitFilter()
{
return true;
}
What I found is that the PDF is actually downloaded twice when it is loaded. In any case, there might be header information and what not at the beginning of the page. When I get a stream segment that begins with %PDF, I know it is the beginning of a PDF so I delete the file to discard any previous contents that might be there. Otherwise, I just keep appending each segment to the end of the file. Theoretically, the PDF file will be safe until you navigate to another PDF, but my recommendation is to do something with the file as soon as the page is loaded just to be safe.

OneDrive REST API Download file

I'm using the REST API of OneDrive in my WCF Web Service. Everything works well but the Download of a file. I need to get the Stream object of the file downloaded but MemoryStream class gives me an Exception about ReadTimeout and WriteTimeout.
This is the code:
.... some code ....
var rClient = new RestClient("https://apis.live.net/v5.0/");
var rRequest = new RestRequest(rootFile.id + "/content", Method.GET);
rRequest.AddParameter("access_token", data.accessToken);
var rResponse = rClient.Execute(rRequest); // THE RESPONSE IS OK
byte[] array = rResponse.RawBytes;
Stream stream = new MemoryStream(array); // PROBLEM HERE!
return stream;
So when I create the Stream Object the MemoryStream throw 2 Exception on the fields ReadTimeout and WriteTimeout saying that they are not supported for this stream.
I don't know how to solve it
As suggested by Will in the comment, I discovered that the Exception on ReadTimeout and WriteTimeout was not the real problem. The Exception was thrown by a null object in the caller method, after the code posted above.
Below there is the point where the Exception was thrown: the Current object was null.
stream = client.DownloadFile(token);
if (stream != null)
{
**WebOperationContext.Current.OutgoingResponse.ContentType = "text/octet-stream";** //HERE
return stream;
}
I removed the line and everything was fixed

Winrt StreamWriter & StorageFile does not completely Overwrite File

Quick search here yielded nothing. So, I have started using some rather roundabout ways to use StreamWriter in my WinRT Application. Reading works well, writing works differently. What' I'm seeing is that when I select my file to write, if I choose a new file then no problem. The file is created as I expect. If I choose to overwrite a file, then the file is overwritten to a point, but the point where the stream stops writing, if the original file was large, then the old contents exist past where my new stream writes.
The code is as such:
public async void WriteFile(StorageFile selectedFileToSave)
{
// At this point, selectedFileToSave is from the Save File picker so can be a enw or existing file
StreamWriter writeStream;
Encoding enc = new UTF8Encoding();
Stream dotNetStream;
dotNetStream = await selectedFileToSave.OpenStreamForWriteAsync();
StreamWriter writeStream = new StreamWriter(dotNetStream, enc);
// Do writing here
// Close
writeStream.Write(Environment.NewLine);
await writeStream.FlushAsync();
await dotNetStream.FlushAsync();
}
Can anyone offer clues on what I could be missing? There are lots of functions missing in WinRT, so not really following ways to get around this
Alternatively you can set length of the stream to 0 with SetLength method before using StreamWriter:
var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);
using (var writer = new StreamWriter(stream))
{
writer.Write(text);
}
Why not just use the helper methods in FileIO class? You could call:
FileIO.WriteTextAsync(selectedFileToSave, newTextContents);
If you really need a StreamWriter, first truncate the file by calling
FileIO.WriteBytesAsync(selectedFileToSave, new byte[0]);
And then continue with your existing code.

Closing stream after using BitmapEncoder with WinJS Metro app

In a Windows 8 Metro application written in JS I open a file, get the stream, write some image data to it using the 'promise - .then' pattern. It works fine - the file is successfully saved to the file system, except after using the BitmapEncoder to flush the stream to the file, the stream is still open. ie; I can't access the file until I kill the application, but the 'stream' variable is out of scope for me to reference, so I can't close() it. Is there something comparable to the C# using statement that could be used?
...then(function (file) {
return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function (stream) {
//Create imageencoder object
return Imaging.BitmapEncoder.createAsync(Imaging.BitmapEncoder.pngEncoderId, stream);
})
.then(function (encoder) {
//Set the pixel data in the encoder ('canvasImage.data' is an existing image stream)
encoder.setPixelData(Imaging.BitmapPixelFormat.rgba8, Imaging.BitmapAlphaMode.straight, canvasImage.width, canvasImage.height, 96, 96, canvasImage.data);
//Go do the encoding
return encoder.flushAsync();
//file saved successfully,
//but stream is still open and the stream variable is out of scope.
};
This simple imaging sample from Microsoft might help. Copied below.
It looks like, in your case, you need to declare the stream before the chain of then calls, make sure you don't name-collide with your parameter to your function accepting the stream (note the part where they do _stream = stream), and add a then call to close the stream.
function scenario2GetImageRotationAsync(file) {
var accessMode = Windows.Storage.FileAccessMode.read;
// Keep data in-scope across multiple asynchronous methods
var stream;
var exifRotation;
return file.openAsync(accessMode).then(function (_stream) {
stream = _stream;
return Imaging.BitmapDecoder.createAsync(stream);
}).then(function (decoder) {
// irrelevant stuff to this question
}).then(function () {
if (stream) {
stream.close();
}
return exifRotation;
});
}