Translate
Monday, October 7, 2013
Send bytes to web client as download
//Setup HttpResponse headers
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "mime-type";
Response.AddHeader("Content-Disposition", "attachment;filename=a_filename.ext");
// Put pdf in OutputStream
//pdf.Save(Response.OutputStream);
// Put image in OutputStream
//bitmap.Save(Response.OutputStream);
// Put bytes in OutputStream
//Response.OutputStream.Write(Bytes[], 0, Bytes.Length);
//Response.BinaryWrite(Bytes[]);
// Put string in OutputStream
//Response.Write(System.Text.Encoding.ASCII.GetString(Bytes[]));
// Send to client
Response.End();
//Response.Flush();
Retrieve File / Sql Contents
// convert StreamReader to string
string path = Server.MapPath("filename");
if (System.IO.File.Exists(path)
{
System.IO.StreamReader streamReader = new System.IO.StreamReader(path);
//System.IO.StreamReader streamReader = System.IO.File.OpenText(path);
//System.IO.FileInfo fi = new System.IO.FileInfo(path);
//System.IO.StreamReader streamReader = fi.OpenText();
}
// place string here
string html = streamReader.ReadToEnd();
// BinaryReader
// Determine buffer size
//System.IO.FileInfo fi = new System.IO.FileInfo(path);
//long bufLen = fi.Length
int chunkSize = 1024;
byte[] buffer = new byte[chunkSize];
//byte[] buffer = new byte[bufLen];
using (System.IO.BinaryReader binaryRdr = new System.IO.BinaryReader(streamReader.BaseStream))
{
buffer = binaryReader.ReadBytes(chunkSize);
while (buffer.Length > 0)
{
buffer = binaryReader.ReadBytes(chunkSize);
}
/* For undetermined sizes
using (var ms = new MemoryStream())
{
streamReader.BaseStream.CopyTo(ms);
buffer = ms.ToArray();
}
*/
}
// SQL
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select columns from Table");
System.Data.SqlClient.SqlDataReader dataRdr;
con.Open();
dataRdr = cmd.ExecuteReader();
byte[] bytes = null;
b=(byte[])dataRdr.GetValue(0);
//OR
b=(byte[])dataRdr[0];
string path = Server.MapPath("filename");
if (System.IO.File.Exists(path)
{
System.IO.StreamReader streamReader = new System.IO.StreamReader(path);
//System.IO.StreamReader streamReader = System.IO.File.OpenText(path);
//System.IO.FileInfo fi = new System.IO.FileInfo(path);
//System.IO.StreamReader streamReader = fi.OpenText();
}
// place string here
string html = streamReader.ReadToEnd();
// BinaryReader
// Determine buffer size
//System.IO.FileInfo fi = new System.IO.FileInfo(path);
//long bufLen = fi.Length
int chunkSize = 1024;
byte[] buffer = new byte[chunkSize];
//byte[] buffer = new byte[bufLen];
using (System.IO.BinaryReader binaryRdr = new System.IO.BinaryReader(streamReader.BaseStream))
{
buffer = binaryReader.ReadBytes(chunkSize);
while (buffer.Length > 0)
{
buffer = binaryReader.ReadBytes(chunkSize);
}
/* For undetermined sizes
using (var ms = new MemoryStream())
{
streamReader.BaseStream.CopyTo(ms);
buffer = ms.ToArray();
}
*/
}
// SQL
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select columns from Table");
System.Data.SqlClient.SqlDataReader dataRdr;
con.Open();
dataRdr = cmd.ExecuteReader();
byte[] bytes = null;
b=(byte[])dataRdr.GetValue(0);
//OR
b=(byte[])dataRdr[0];
Labels:
c-sharp,
System.IO,
Transact-Sql