How to close window with apsx page? - c#

I have a aspx page in window. Now i want to close it from cs code:
string result = #"<script type='text/javascript'>
window.returnValue = true;
window.close();
</script>";
Response.Write(result);
It's work fine.
Now i want to save a file and then close a window. And my code from above not work. I get a save file dialog, save a fine, but my window not close.
What i should do in this case?
SAVE FILE CODE
File.WriteAllBytes(docFileName, documentStream.ToArray());
fi = new FileInfo(docFileName);
if (fi.Exists)
{
byte[] buffer;
using (FileStream fileStream = new FileStream(docFileName, FileMode.Open))
{
int fileSize = (int)fileStream.Length;
buffer = new byte[fileSize];
// Read file into buffer
fileStream.Read(buffer, 0, fileSize);
fileStream.Close();
fi.Delete();
}
Response.Clear();
Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + docFileName);
Response.CacheControl = "public";
// writes buffer to OutputStream
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.End();

You cannot close the window like that.
You can only close the windows that was created by the script.

Related

Not able to download and parallelly open a file object stored in server in asp.net

I have tried to download and opened a file object stored in Server, but not able to open the file post download. Please find the sample code I am trying as below. I have used Content-Disposition to inline, but still it is not working as mentioned in other solution to this problem. It just downloads, but don't open. Any help please on this?
protected void Page_Load(object sender, EventArgs e)
{
string filePath = this.Server.MapPath("~/Scripts/48A4-4972-AEBE-8E7C11AE64AF");
string fileName = "crma.jpg";
FileStream stream = null;
HttpResponse response = HttpContext.Current.Response;
long dataToRead;
try
{
stream = new FileStream(filePath, FileMode.Open);
dataToRead = stream.Length;
response.ClearContent();
response.AddHeader("Content-Disposition", "inline; filename=" + "\"" + fileName + "\"");
response.AddHeader("Content-Length", dataToRead.ToString());
response.ContentType = "application/jpg";
byte[] buffer = new Byte[10000];
while (dataToRead > 0)
{
if (response.IsClientConnected)
{
int length = stream.Read(buffer, 0, 10000);
response.OutputStream.Write(buffer, 0, length);
response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
finally
{
if (stream != null)
stream.Close();
response.End();
response.Close();
}
}

Display .jpg / .png image from folder in browser instead of downloading it ASP.NET

I am trying to display .png / .jpg image in browser but the problem is instead of displaying the image in the browser it is getting downloaded. I have tried using content-disposition:inline as well but its downloading the complete aspx page. Can anyone help me with this. Below is my code
string filePath = "C:\\inetpub\\wwwroot\\Folder\\OSP\\20139000\\";
string filename = "test.jpg";
string contenttype = "image/" +
Path.GetExtension(filename.Replace(".", ""));
FileStream fs = new FileStream(filePath + filename,
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
////Write the file to response Stream
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contenttype;
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
You miss
Response.Clear();
somewhere at the beginning of the script. Without clearing the response's buffer first, it already contains the text of the *.aspx page the script is run under.
Then, don't set the content's disposition to attachment, completely delete this line.

PDFsharp not giving pdf file in response

I am working on export functionality using PDFsharp in .Net MVC I am not getting any error in my code but not getting PDF file in response.
I have tried doing it manually writing it to specific path with the help of : System.IO.File.WriteAllBytes(path1, bytes); and it's working perfectly, but I am not getting PDF in Response with the help of
Response.BinaryWrite(bytes);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Anyone have faced this type of issue or someone from community please help
Here is my code :
public bool ExportPdf(string htmlcontenttbl)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=Myfile.pdf");
Response.ContentType = "application/pdf";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PdfDocument document =
PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4,
30);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PageSize.A4;
config.MarginBottom = 30;
config.MarginTop = 30;
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, true);
bytes = stream.ToArray();
}
var path1 = Server.MapPath("~/Images/" +
DateTime.Now.TimeOfDay.Ticks + "result.pdf");
//System.IO.File.WriteAllBytes(path1, bytes);
//Response.BinaryWrite(bytes);
//Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.Flush();
Response.End();
return true;
}
thank you,

downloaded pdf file not opening

I am saving a pdf file in the database by the following code
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filetype = Path.GetExtension(FileUpload1.PostedFile.FileName);
int filesize = FileUpload1.PostedFile.ContentLength;
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] content = br.ReadBytes((Int32)fs.Length);
Objects.Insert_FilesToDatabase(filename, filetype, content,filesize);
and then, Iam trying to save the file from the database by clicking the link thru the following code.
void lnkDownload_Click(object sender, EventArgs e)
{
string filetype = Objects.GetFileType(Convert.ToInt32(txtslno.Text.Trim()));
string filename=Objects.GetFileName(Convert.ToInt32(txtslno.Text.Trim()));
int filesize = Objects.GetFileLength(Convert.ToInt32(txtslno.Text.Trim()));
byte[] bytfile = new byte[filesize+1000];
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",attachment;filename="+filename+".pdf");
Response.BufferOutput = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytfile);
Response.End();
}
Thru this code, I am able to download the pdf file but I am unable to open the pdf file. The error is the file is not decoded properly. Can you help me as to where am I going wrong?
I solved this problem thru the following code..
byte[] bytfile = Objects.GetFile(Convert.ToInt32(txtslno.Text.Trim()));
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename="+filename);
Response.AddHeader("Content-Length", bytfile.Length.ToString());
Response.OutputStream.Write(bytfile, 0, bytfile.Length);
Response.Flush();
Response.End();
I am just not writing the binary content to the output pdf stream in my previous code..
Thank you for your support

Website stops responding after sending file to user

I created a site with C# which can download files. after calling
//A webservice returning a stream with the document data.
Stream checkOut = FileProxy.RetrieveDocument(Ticket, ID);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.BufferOutput = true;
HttpContext.Current.Response.ContentType = "application/x-download";
HttpContext.Current.Response.AddHeader("Content-Disposition", "atachment; filename=" + filenamevariable);
HttpContext.Current.Response.CacheControl = "public";
byte[] buffer = new byte[255];
int butesRead = 0;
while ((bytesread = checkOut.Read(buffer, 0, 255)) > 0)
{
if(HttpContext.Current.REsponse.IsClientConnected)
{
HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length);
HttpContext.Current.Response.Flush();
}
}
Httpcontext.Current.Response.Close();
checkOut.Close();
Running this code succesfully downloads the document but i can not continue using the site (Buttons don't work anymore). Am i forgetting something after the download completion?

Categories