memorystream

How to set the name of the file when streaming a Pdf in a browser?

人走茶凉 提交于 2019-12-05 02:25:33
Not sure exactly how to word this question ... so edits are welcomed! Anyway ... here goes. I am currently use Crystal Reports to generated Pdfs and just stream the output to the user. My code looks like the following: System.IO.MemoryStream stream = new System.IO.MemoryStream(); stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); this.Response.Clear(); this.Response.Buffer = true; this.Response.ContentType = "application/pdf"; this.Response.BinaryWrite(stream.ToArray()); this.Response.End(); After this code runs it streams

DeflateStream doesnt work on MemoryStream?

你离开我真会死。 提交于 2019-12-05 01:19:02
I have the following piece of code: MemoryStream resultStream = new MemoryStream(); string users = ""//Really long string goes here BinaryFormatter bFormatter = new BinaryFormatter(); using (MemoryStream assignedUsersStream = new MemoryStream()) { bFormatter.Serialize(assignedUsersStream, users); assignedUsersStream.Position = 0; using (var compressionStream = new DeflateStream(resultStream, CompressionLevel.Optimal)) { assignedUsersStream.CopyTo(compressionStream); Console.WriteLine("Compressed from {0} to {1} bytes.", assignedUsersStream.Length.ToString(), resultStream.Length.ToString()); }

Decompress byte array to string via BinaryReader yields empty string

一笑奈何 提交于 2019-12-04 21:22:19
问题 I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string. BinaryReader br = null; string str = String.Empty; using (MemoryStream inStream = new MemoryStream(pByteArray)) { GZipStream zipStream = new GZipStream(inStream, CompressionMode.Decompress); BinaryReader br = new BinaryReader(zipStream); str = br.ReadString(); inStream.Close(

What is the best practice for storing a file upload to a MemoryStream (C#)?

回眸只為那壹抹淺笑 提交于 2019-12-04 18:13:59
I need to temporary store a file upload to a MemoryStream. What would be the best method to do this using asp.net (3.5)? Here's what I have so far. It works (locally tested), but it does not look right to me. protected void lnkUploadFile_Click(object sender, EventArgs e) { MemoryStream memStream = new MemoryStream(); BinaryWriter sWriter = new BinaryWriter(memStream); foreach (byte b in flUpload.FileBytes) { sWriter.Write(b); } sWriter.Flush(); // writing to file to verify file stream converted correctly FileStream fstream = new FileStream(@"C:/test/" + flUpload.FileName, FileMode.Create);

How can I read from memory just like from a file using wistream?

倖福魔咒の 提交于 2019-12-04 16:41:59
In my previous question I asked how to read from a memory just as from a file. Because my whole file was in memory I wanted to read it similarly. I found answer to my question but actually I need to read lines as a wstring . With file I can do this: wifstream file; wstring line2; file.open("C:\\Users\\Mariusz\\Desktop\\zasoby.txt"); if(file.is_open()) { while(file.good()) { getline(file,line2); wcout << line2 << endl; } } file.close(); Even if the file is in ASCII. Right now I'm simply changing my string line to wstring with a function from this answer. However, I think if there is a way to

Get Imagesource from Memorystream in c# wpf

让人想犯罪 __ 提交于 2019-12-04 15:56:16
问题 How can I get ImageSource from MemoryStream in WPF using c# ? or convert MemoryStream to ImageSource to display it as image in wpf ? 回答1: using (MemoryStream memoryStream = ...) { var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.StreamSource = memoryStream; imageSource.EndInit(); // Assign the Source property of your image image.Source = imageSource; } 来源: https://stackoverflow.com/questions/6588974/get-imagesource-from-memorystream-in-c-sharp-wpf

C# 3.0 Save itextsharp pdf to database using MemoryStream

Deadly 提交于 2019-12-04 14:55:33
I'm trying to save to databse a pdf file generated by itextsharp. But, I haven't been successfully so far. I'm using Linq to sql. Here's the code: MemoryStream ms = new MemoryStream(); Document d = new Document(PageSize.A4, 60, 60, 40, 40); PdfWriter w = PdfWriter.GetInstance(d, ms); w.CloseStream = false; string txtTemplate = ""; Encoding en = Encoding.GetEncoding("iso-8859-1"); StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Content/templates/CessaoDireitosDica.txt"), en); txtTemplate = sr.ReadToEnd(); sr.Close(); string conselhos = ""; Font font = new Font(Font

Equivalent of Java's “ByteBuffer.putType()” in C#

会有一股神秘感。 提交于 2019-12-04 14:08:19
I am trying to format a byte array in C#, by porting a code from Java. In Java, the methods "buf.putInt(value);", buf.putShort, buf.putDouble, (and so forth) are used. However I don't know how to port this to C#. I have tried the MemoryStream class, but there is no method to put a specific type at the end of the byte array. Question: What is the equivalent of Java's "ByteBuffer.putType(value)" in C#? Thanks! You can use a BinaryWriter and your MemoryStream: MemoryStream stream = new MemoryStream(); using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(myByte); writer.Write

Do I need to do StreamWriter.flush()?

╄→гoц情女王★ 提交于 2019-12-04 08:51:55
问题 Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter = new StreamWriter(stream); BinaryWriter binaryWriter = new BinaryWriter(stream); foreach(...) { binaryWriter.Write(number); normalWriter.WriteLine(name); //<~~ easier to reader afterward. } return MemoryStream.ToArray(); } My questions are: Do I need to use flush inside the loop to preserve order? Is returning MemoryStream.ToArray() legal? I using the using -block as a convention, I'm afraid it

C# to Java: Base64String, MemoryStream, GZipStream

不羁岁月 提交于 2019-12-04 07:14:15
I have a Base64 string that's been gzipped in .NET and I would like to convert it back into a string in Java. I'm looking for some Java equivalents to the C# syntax, particularly: Convert.FromBase64String MemoryStream GZipStream Here's the method I'd like to convert: public static string Decompress(string zipText) { byte[] gzipBuff = Convert.FromBase64String(zipText); using (MemoryStream memstream = new MemoryStream()) { int msgLength = BitConverter.ToInt32(gzipBuff, 0); memstream.Write(gzipBuff, 4, gzipBuff.Length - 4); byte[] buffer = new byte[msgLength]; memstream.Position = 0; using