File download - mvc2

I have used infomentums activefile in the past with classic asp. They have version 2.5 available but i can’t download it from their website and i am afraid of downloading it from third parties like cnet etc due to exes having infections. Looking for recommendations for a good component, if it can help with file upload with status message, that would be great.

I have written my own and it is downloading the file but the file is coming up as garbage. Windows is asking me for the program to open it.


 [COLOR=blue][FONT=Consolas]public[/FONT][/COLOR][FONT=Consolas] [COLOR=blue]byte[/COLOR][] GetFileBytes([COLOR=blue]string[/COLOR] destinationFilePath)[/FONT]
  [FONT=Consolas]        {[/FONT]
  [FONT=Consolas]            [COLOR=blue]byte[/COLOR][] fileBytes = [COLOR=blue]null[/COLOR];[/FONT]
    [FONT=Consolas]            NWImpersonate.Impersonate(); //**** Need to impersonate
[/FONT]
  [FONT=Consolas]            [COLOR=blue]if[/COLOR] ([COLOR=#2B91AF]File[/COLOR].Exists(destinationFilePath))[/FONT]
  [FONT=Consolas]            {[/FONT]
  [FONT=Consolas]                fileBytes = [COLOR=#2B91AF]File[/COLOR].ReadAllBytes(destinationFilePath);[/FONT]
  [FONT=Consolas]            }[/FONT]
  [FONT=Consolas] [/FONT]
  [FONT=Consolas]            NWImpersonate.Dispose();[/FONT]
  [FONT=Consolas]            [/FONT]
  [FONT=Consolas]            [COLOR=blue]return[/COLOR] fileBytes;[/FONT]
  [FONT=Consolas]        }[/FONT]
  

Use


FileSystem fso = new FileSystem();
byte[] fileBytes = fso.GetFileBytes(filePath);
                    if (fileBytes != null)
                    {
                        result = new FileContentResult(fileBytes, "application/octet-stream");
                    }

Thanks

My first guess would be to not use “application/octet-stream”.

Thanks, i’ll try that.

How can i push the file out with a different name than with which it is available?

Check the third overload.

This worked like a charm, thanks for your help imaginekitty.


result = new FileContentResult(fileBytes, "application/octet-stream") { FileDownloadName = fileName };

I have experimented with below code but would still prefer to use the above code. Followed a tutorial which i am not able to find again (making sure that i don’t get credit for this code).

Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Core.MvcBase
{
    //download file
    public class BinaryContentResult : ActionResult
    {
        #region CONSTANTS

        public const string ApplicationOctetStream = "application/octet-stream";

        #endregion


        public BinaryContentResult()

        { }

        public string ContentType { get; set; }
        public string FileName { get; set; }
        public byte[] FileBytes { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ClearContent();
            context.HttpContext.Response.ContentType = ContentType;
            context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
            context.HttpContext.Response.BinaryWrite(FileBytes);
            context.HttpContext.Response.End();
        }
    }
}

use


result = new BinaryContentResult(){ 
                                                            FileBytes = fileBytes, 
                                                            ContentType = BinaryContentResult.ApplicationOctetStream, 
                                                            FileName = fileName
                                                            };

This resulted in an error

… does not contain a constructor that takes 3 parameters…

image attached…

Actually the third overload is in the FileContentResult like this:

result = new FileContentResult(fileBytes, "application/pdf", "newFileName.pdf");

Are you still using “octet-stream”?

I’m sorry, made a mistake try

public FileContentResult filez()
{
	return File(fileBytes, "application/pdf", "newFileName.pdf");
}

return a File() not a FileContentResult();

Sorry for the mixup.

Thanks, just tested it and this worked great. Going to keep the File() as it is cleaner than FileContentResult().

Need an advice, is there a better approach available than first converting the file into byte array? In our case, we need to impersonate first before we get access to the file.

Thanks

Yes, use FilePathResult.

Why do you need to impersonate? What is the purpose?

Thanks, i’ll look into FilePathResult.

These files are sitting on the file server. Anonymous users can’t get to these files. No virtual paths are specified so that is not available in this case also. It’s a security measure…