Dynamically Create PDF file and Bind to Pdf Viewer control - c#

I am generating an ssrs report in my Silverlight application and I need to convert the ssrs report to RadPDFViewer control(telerik silverlight).
So I am saving the pdf file in the project folder using WCF.
Now i need to read the file again and bind it to Pdfviewer.
Saving happens asynchronously.
How should I wait untill the file is saved and then read from the folder?
Also, can you please show me how to read the PDF as a Memory stream.
I run the following code unsuccessfully.
public byte[] ReturnPdf(string requestUrl) {
HttpWebRequest req = null; var buf = new byte[1024];
try
{
req = (HttpWebRequest)WebRequest.Create(requestUrl);
req.Credentials = CredentialCache.DefaultCredentials;
req.Method = "GET"; var objResponse = req.GetResponse();
var stream = objResponse.GetResponseStream();
if (stream != null){BinaryReader br = new BinaryReader(stream);
buf = br.ReadBytes(1024);
} if
(stream != null) stream.Close();
}
catch Exception e){}return buf;
}
private void button2_Click(object sender, EventArgs e)
{
string baseUrl = "http://abc/ReportServer&rs:Command=Render&rs:ClearSession=true&rs:Format=PDF";
const string nullString = ":isnull=true";
byte[] o = ReturnPdf(baseUrl);
byte[] bytes = new byte[1024];
Stream s = new MemoryStream(bytes);
}

Write PDF Stream:
Write PDF stream to response stream
You could check to see if the file has been written completely.
How to test if a file is currently being written to
Or this might help:
How to check for file lock?

Related

Download CSV file with WebClient in C# but the size of file is less than when download with browser

I have a link that returns a CSV file. When I open it in a browser (Chrome, Firefox,...) the size of file that's downloaded is 86 KB, but when I want to download it with the code below, the size is just 25 KB and when I open the downloaded file it doesn't have correct data (means no columns and can't read data)
You can try it in browser and code
http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462
string url = "http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462";
WebClient wc = new WebClient();
wc.DownloadFile(url, "111.csv");
webClient is returning you zip file instead of plain text /csv file
I changed wc output file extension to zip and it is working...
zip will contain file that you specified in argument
screenshot from RestClient
As Akshay Sandhu pointed out the downloaded file is compressed with the gzip encoding and that is why it appears as corrupted when trying to open it as a csv.
To download the file and automatically decode it please refer to these two SO answers.
First download the file using the HttpWebRequest class instead of the WebClient class as done here:
How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)
Then make sure the file is automatically decompressed. Check this out
Automatically decompress gzip response via WebClient.DownloadData
Here is the working code:
string url = "http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462";
string path = "111.csv";
using (FileStream fileStream = new FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
const int BUFFER_SIZE = 16 * 1024;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
fileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}
You need to decompress the gzip, before you read it to file.
var url = new Uri("http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462");
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var fileName = "111.csv";
using (WebClient wc = new WebClient())
using (Stream s = File.Create(Path.Combine(path, fileName)))
using (GZipStream gs = new GZipStream(wc.OpenRead(url), CompressionMode.Decompress))
{
//Saves to C:\Users\[YourUser]\Desktop\111.csv
gs.CopyTo(s);
}

c# files downloaded with httpwebrequest and cookies get corrupted

I am trying to make a program which is able to download files with URI(URL) using httpwebrequest and cookies(for credential information to keep login status).
I can download files with following code but files get corrupted after being downloaded.
when I download xlsx file(on the web page) into text file at local drive, I see some part of numbers and words from an original file in a corrupted file, therefore I assume I have reached to the right file.
however, when I download xlsx file(on the web page) in xlsx file at local drive, it seems like it fails to open saying
excel cannot open the file 'filename.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and that the file extension matches the format of the file.
Is there any way I can keep fully original file content after I download?
I attach a part of result content as well.
private void btsDownload_Click(object sender, EventArgs e)
{
try
{
string filepath1 = #"PathAndNameofFile.txt";
string sTmpCookieString = GetGlobalCookies(webBrowser1.Url.AbsoluteUri);
HttpWebRequest fstRequest = (HttpWebRequest)WebRequest.Create(sLinkDwPage);
fstRequest.Method = "GET";
fstRequest.CookieContainer = new System.Net.CookieContainer();
fstRequest.CookieContainer.SetCookies(webBrowser1.Document.Url, sTmpCookieString);
HttpWebResponse fstResponse = (HttpWebResponse)fstRequest.GetResponse();
StreamReader sr = new StreamReader(fstResponse.GetResponseStream());
string sPageData = sr.ReadToEnd();
sr.Close();
string sViewState = ExtractInputHidden(sPageData, "__VIEWSTATE");
string sEventValidation = this.ExtractInputHidden(sPageData, "__EVENTVALIDATION");
string sUrl = ssItemLinkDwPage;
HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(sUrl);
hwrRequest.Method = "POST";
string sPostData = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" + sViewState + "&__EVENTVALIDATION=" + sEventValidation + "&Name=test" + "&Button1=Button";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bByteArray = encoding.GetBytes(sPostData);
hwrRequest.ContentType = "application/x-www-form-urlencoded";
Uri convertedURI = new Uri(ssDwPage);
hwrRequest.CookieContainer = new System.Net.CookieContainer();
hwrRequest.CookieContainer.SetCookies(convertedURI, sTmpCookieString);
hwrRequest.ContentLength = bByteArray.Length;
Stream sDataStream = hwrRequest.GetRequestStream();
sDataStream.Write(bByteArray, 0, bByteArray.Length);
sDataStream.Close();
using (WebResponse response = hwrRequest.GetResponse())
{
using (sDataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(sDataStream);
{
string sResponseFromServer = reader.ReadToEnd();
FileStream fs = File.Open(filepath1, FileMode.OpenOrCreate, FileAccess.Write);
Byte[] info = encoding.GetBytes(sResponseFromServer);
fs.Write(info, 0, info.Length);
fs.Close();
reader.Close();
sDataStream.Close();
response.Close();
}
}
}
}
catch
{
MessageBox.Show("Error");
}
}
StreamReader is for dealing with text data. Using it corrupts your binary data(excel file).
Write sDataStream directly to file. For ex.
sDataStream.CopyTo(fs)
PS: I prepared a test case (using similar logic) to show how your code doesn't work
var binaryData = new byte[] { 128,255 };
var sr = new StreamReader(new MemoryStream(binaryData));
var str3 = sr.ReadToEnd();
var newData = new ASCIIEncoding().GetBytes(str3); //<-- 63,63
Just compare binaryData with newData

Exporting byte[] results to corrupted .zip

I am trying to make a CLR with .NET 2.0 integrated into MS SQL Server 2008. I call an API with and I should receive a .zip as response. I store the response into a Stream and I want to export this file to a physical .zip file.
I tried exporting the file with C# and SQL (BCB OR OLE) and all resulted into a corrupted file. So, I believe I am doing something wrong with in the making of the stream.
The C# code is the following:
private static byte[] GetStreamFileResult(Cookie loginCookie, Guid fileGuid, String baseUri)
{
byte[] output = null;
//String result = null;
string url = "some url"
CookieContainer cookies = new CookieContainer();
cookies.Add(new Uri(url), loginCookie);
WebRequest request = WebRequest.Create(url);
(request as HttpWebRequest).CookieContainer = cookies;
WebResponse response = request.GetResponse();
HttpWebResponse resp = response as HttpWebResponse;
Stream dataStream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
CopyStream(dataStream, ms);
output = ms.ToArray();
}
dataStream.Close();
response.Close();
return output;
}
The C# code to export the zip is the following:
File.WriteAllBytes("C:\\folder\\t.zip", stream); // Requires System.IO
The copy from stream to stream:
public static void CopyStream(Stream input, Stream output)
{
if (input != null)
{
using (StreamReader reader = new StreamReader(input))
using (StreamWriter writer = new StreamWriter(output))
{
writer.Write(reader.ReadToEnd());
}
}
}
Your CopyStream is broken. You need to talk binary. You're currently treating binary zip data as though it were text:
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, bytesRead);
}

Forward posted image to WCF Rest Service

I have a webform application written in C# and what I want to do is after the user submits a HTML form with an image, I send that image to a WCF Rest service also written in C#.
The problem is when I get the image in the web service this is corrupted.
I guess that the problem is that I don't encode the file properly, but after days of reading on internet I haven't found a clue.
Webform code:
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile image = Request.Files["imagen"];
string serverResponse = Send("mywebservice/postimage", "POST", Encoding.UTF8.GetBytes(StreamToString(image.InputStream)));
}
Edit (this way worked)
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile image = Request.Files["imagen"];
MemoryStream ms = new MemoryStream();
image.InputStream.CopyTo(ms);
byte[] bytes = ms.ToArray();
string serverResponse = Send("mywebservice/postimage", "POST", bytes);
}
public int Send(string url, string method, byte[] data)
{
string serverResponse = "";
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(url);
newRequest.ContentType = "image/jpeg";
newRequest.Method = method;
newRequest.Timeout = 10000;
if (newRequest.Method == "POST" || newRequest.Method == "PUT")
{
Stream reqStream = newRequest.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
WSMessageEnt wsMessageEnt = new WSMessageEnt();
try
{
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)newRequest.GetResponse();
Stream dataStream = webResponse.GetResponseStream();
serverResponse = new StreamReader(dataStream).ReadToEnd();
}
catch (WebException we)
{
}
return serverResponse;
}
public static string StreamToString(Stream data)
{
StreamReader reader = new StreamReader(data);
string body = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return body;
}
Webservice code:
[WebInvoke(UriTemplate = "upload-user-image", Method = "POST")]
public Stream UploadUserImage(Stream streamdata)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(streamImagen, true);
// here I get a format error
}
Possibly one problem: you're converting a stream of bytes, into a string, then back to a stream of bytes. The image bytes are arbitrary bytes, which may or may not map to a string, and the encoding you're using in one conversion (Encoding.Default) may not be the same as the one used in the other (Encoding.UTF8).
Instead of doing this:
Encoding.UTF8.GetBytes(StreamToString(image.InputStream))
Try doing something like
MemoryStream ms = new MemoryStream();
image.InputStream.CopyTo(ms);
byte[] bytes = ms.ToArray();
Or just pass the stream to Send and copy it to the request stream.

download zip files by use of reader in c#

I have been working on this application that enables user to log in into another website, and then download specified file from that server. So far I have succeeded in logging on the website and download the file. But everything ruins when it comes to zip files.
Is there any chunk of code that could be helpful in reading the .zip files byte by byte or by using stream reader?
I m using downloadfile() but its not returning the correct zip file.
I need a method by which I can read zip files. Can I do it by using ByteReader()
The code used to download zip file is
string filename = "13572_BranchInformationReport_2012-05-22.zip";
string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString();
WebClient client = new WebClient();
string user = "abcd", pass = "password";
client.Credentials = new NetworkCredential(user, pass);
client.Encoding = System.Text.Encoding.UTF8;
try
{
client.DownloadFile("https://web.site/archive/13572_BranchInformationReport_2012-05-22.zip", filepath);
Response.Write("Success");
}
catch (Exception ue)
{
Response.Write(ue.Message);
}
Thanks in advance.
is there any chunk of code that could be helpful in reading the zip files bytes by bytes aur by using stream reader.
Absolutely not. StreamReader - and indeed any TextReader is for reading text content, not binary content. A zip file is not text - it's composed of bytes, not characters.
If you're reading binary content such as zip files, you should be using a Stream rather than a TextReader of any kind.
Note that WebClient.DownloadFile and WebClient.DownloadData can generally make things easier for downloading binary content.
Another simple way to downlaod zip file
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/DOWNLOAD/Filename.zip">Click To Download</asp:HyperLink>
Another solution
private void DownloadFile()
{
string getPath = "DOWNLOAD/FileName.zip";
System.IO.Stream iStream = null;
byte[] buffer = new Byte[1024];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = Server.MapPath(getPath);
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
// Page.Response.ContentType = "application/vnd.android.package-archive";
// Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 1024);
// Write the data to the current output stream.
Page.Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Page.Response.Flush();
// buffer = new Byte[1024];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Page.Response.Write(ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
Page.Response.Close();
}
}
}
Your answer
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
byte[] buffer = new byte[32768];
using (Stream input = objResponse.GetResponseStream())
{
using (FileStream output = new FileStream ("test.doc",
FileMode.CreateNew))
{
int bytesRead;
while ( (bytesRead=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
This is how i achieved it. Thanks everyone for ur help

Categories

Resources