Pregunta

I have developed a website using Asp.net. In a section of my program users need to download some variable files. (Jpeg,Pdf,doc,docx...) As they push the download button and hit "Open" button instead of "save" button, the file will open with Adobe Acrobat. The problem is that if it is a word file, again it will be open with Adobe Acrobat! How can i avoid this and each file open with it's own program? I used this cod in my behind cod:

   protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        int idx = Convert.ToInt32(e.CommandArgument);
        string Manuscript = GridView2.DataKeys[idx].Value.ToString();

        string fileName = System.IO.Path.GetFileName(Manuscript);

        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.TransmitFile(Server.MapPath(Manuscript));
        Response.End(); 
    }
¿Fue útil?

Solución

Like this :

string ext = System.IO.Path.GetExtension(this.File1.PostedFile.FileName);
string contenttype = String.Empty;

OR

string ext = Path.GetExtension(filename);

switch(ext)
{
    case ".doc":
        contenttype = "application/vnd.ms-word";
        break;
    case ".docx":
        contenttype = "application/vnd.ms-word";
        break;
    case ".xls":
        contenttype = "application/vnd.ms-excel";
        break;
    case ".xlsx":
        contenttype = "application/vnd.ms-excel";
        break;
    case ".jpg":
        contenttype = "image/jpg";
        break;
    case ".png":
        contenttype = "image/png";
        break;
    case ".gif":
        contenttype = "image/gif";
        break;
    case ".pdf":
        contenttype = "application/pdf";
        break;
}

Otros consejos

Use

FileInfo fInfo = new FileInfo(fileName);
string fileExtn = fInfo.Extension;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top