jorgehvieirasilva
jorgehvieirasilva

Reputation: 24

Response Redirect not working

private void Button1_OnClick(object sender, EventeArgs e)
{
    Response.Redirect(myselect.SelectedValue.ToString(), true);
}

Above is my code, I already put a breakpoint on .SelectedValue and it's recognizing the value, but when I click on the button it shows this message:

Error Picture

Upvotes: 1

Views: 247

Answers (2)

Hany Habib
Hany Habib

Reputation: 1405

to do what you need, you must download the file to the client. Response.Redirect as people mentioned redirects to URL. To make it open in the browser you need the below :

private void Button1_OnClick(object sender, EventeArgs e)
{

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline; filename=MyFile.pdf");
Response.TransmitFile(myselect.SelectedValue.ToString());
Response.End();
}

For Content-Disposition you have two choices :

Response.AppendHeader("Content-Disposition", "attachment;filename=somefile.ext") : Prompt will appear for file download

Response.AppendHeader("Content-Disposition", "inline;filename=somefile.ext") : the browser will try to open the file within the browser.

Upvotes: 2

Daniel
Daniel

Reputation: 9849

Your sample is assuming that a site e.g. 1.aspx or 221.aspx exists. You are only passing some selected value.

private void Button1_OnClick(object sender, EventeArgs e)
{
    Response.Redirect(myselect.SelectedValue.ToString(), true);
}

you need to redirect to some kind of action like:

public FileResult DownloadFile(int id) {

    // Your code to retrieve a byte array of your file
    var thefileAsByteArray = .....

    return File(thefileAsByteArray, System.Net.Mime.MediaTypeNames.Application.Octet, 'DownloadFilenName.pdf');       
}

Then you would need to change your onClick metho like:

private void Button1_OnClick(object sender, EventeArgs e)
{
     Response.Redirect("Download.aspx?id=" + myselect.SelectedValue.ToString(), true);
}

Upvotes: 1

Related Questions