0

An ASP.NET MVC solution that was working fine in VS 2012 stopped working in 2019 and I cannot find what has changed to break it.

Originally I had the code in the first block - the async task would go to the file storage and retrieve the file, and then the file was sent to the browser using a FileResult that the controller called automatically. After a VERY painful change to VS 2019 and updating everything (.NET runtime, 3rd party libraries, etc.) I have the application working again except for this issue.

I tried creating a new FileStreamResult (which is in the 2nd block) but that does not get called either. When I click on a link that calls this:

    <a href="/Cert/File?folder=&filename=@HttpUtility.UrlEncode(FilePath)" ...

It gives me a blank page instead of downloading the file as it used to.


        public async Task FileAsync(string folder, string filename)
        {
            AsyncManager.OutstandingOperations.Increment();

            var ReadObjectTask = _fileStorageProvider.ReadObjectDataAsync(folder, filename);
            Stream ROResult = await ReadObjectTask;
            AsyncManager.Parameters["stream"] = ROResult;
            AsyncManager.Parameters["filename"] = filename;

            AsyncManager.OutstandingOperations.Decrement();

        }
        public FileResult FileCompleted(Stream stream, string filename)
        {
            if (stream == null)
            {
                return File(Server.MapPath(Url.Content("~/Content/bad_file.png")), "image/png");
            }
            var file = new FileStreamResult(stream, MIMEAssistant.GetMIMEType(filename));
            if (filename.Contains("/"))
            {
                filename = filename.Split('/').Last();
            }
            file.FileDownloadName = filename;
            return file;
        }

Here is the FileStreamResult I tried:

        public System.Web.Mvc.FileStreamResult FileCompleted(Stream stream, string contentType, string filename)
        {
            if (stream == null)
            {
                string bFile = Server.MapPath(Url.Content("~/Content/bad_file.png"));
                Stream blankfile = System.IO.File.OpenRead(bFile);
                return File(blankfile, MIMEAssistant.GetMIMEType(bFile), System.IO.Path.GetFileName(bFile));
            }
            if (filename.Contains("/"))
            {
                filename = filename.Split('/').Last();
            }
            return File(stream, MIMEAssistant.GetMIMEType(filename), filename);
        }

(The filename.Contains part is old code from a predecessor that I just need to replace with Path.GetFileName - sorry I did not clean it up before I posted.)

I decided to make the Async Task one of type and moved the stream processing into that procedure to solve my problem. I do not know why the Async Task that was working in 2012 stopped in 2019.

public async Task<FileResult> FileAsync(string folder, string filename)
        {
            AsyncManager.OutstandingOperations.Increment();

            var ReadObjectTask = _fileStorageProvider.ReadObjectDataAsync(folder, filename);
            Stream ROResult = await ReadObjectTask;
            AsyncManager.Parameters["stream"] = ROResult;
            AsyncManager.Parameters["filename"] = filename;
            AsyncManager.OutstandingOperations.Decrement();

            if (ROResult == null)
            {
                return File(Server.MapPath(Url.Content("~/Content/bad_file.png")), "image/png");
            }
            var file = new FileStreamResult(ROResult, MIMEAssistant.GetMIMEType(filename));
            file.FileDownloadName = System.IO.Path.GetFileName(filename);
            return file;

        }
Tink
  • 29
  • 1
  • 7

0 Answers0