1

Here i am trying to open the file of any extension format in ASP.NET MVC CORE 2.0 that has been saved in data base according to the extension format of that file without downloading that file. For example lets say i have ms word file so when i click that file it should open in word, if i have pdf it should open in pdf format without downloading the file.
Now, the problem in my code is that it force to download the file instead of opening the file according to respective file extemsion format.

Any help will be a great and will be thank full.Thank You

Below is my code

  public IActionResult ViewFileByFileId (int id)
    {
        DoctorCredentialDocsModel DoctorCredential = new DoctorCredentialDocsModel();
        DoctorCredential = _doctorService.GetDoctorCredentialDetails(id);
        string AttachPath = ConfigPath.DoctorCredentialsAttachmentPath;
        string strFileFullPath = Path.Combine(AttachPath, DoctorCredential.AttachedFile);

        string contentType = MimeTypes.GetMimeType(strFileFullPath);
        if (!strFileFullPath.Contains("..\\"))
        {
            byte[] filedata = System.IO.File.ReadAllBytes(strFileFullPath);
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = DoctorCredential.FileName,
                Inline = false,
            };
            Request.HttpContext.Response.Headers.Add("Content-Disposition", cd.ToString());
            return File(filedata, contentType);
        }
        else
        {
            return new NotFoundResult();

        }
    }
redcaper1
  • 41
  • 2
  • how can you open file without downloading it? Do you mean "download and open in browser"? – vasily.sib Aug 15 '18 at 06:36
  • it should open according to there extension format without downloading, it's like returning file to view before download – redcaper1 Aug 15 '18 at 06:37
  • but with `return File(filedata, contentType);` you sending whole file content, how your browser can reject to download it? – vasily.sib Aug 15 '18 at 06:41

1 Answers1

0

Try yo send like tihs. FileContentResult may can help you :)

Lastly look at this :)

What's the difference between the four File Results in ASP.NET MVC

 public FileContentResult  ViewFileByFileId (int id)
    {
        DoctorCredentialDocsModel DoctorCredential = new DoctorCredentialDocsModel();
        DoctorCredential = _doctorService.GetDoctorCredentialDetails(id);
        string AttachPath = ConfigPath.DoctorCredentialsAttachmentPath;
        string strFileFullPath = Path.Combine(AttachPath, DoctorCredential.AttachedFile);

        string contentType = MimeTypes.GetMimeType(strFileFullPath);
        if (!strFileFullPath.Contains("..\\"))
        {
            byte[] filedata = System.IO.File.ReadAllBytes(strFileFullPath);
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = DoctorCredential.FileName,
                Inline = false,
            };
            Request.HttpContext.Response.Headers.Add("Content-Disposition", cd.ToString());
            return File(filedata, contentType);
        }
        else
        {
            return new NotFoundResult();

        }
    }
Eyup Can ARSLAN
  • 692
  • 9
  • 25