• Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)


    Currently, I was trying to write an ASP.Net application that involved a user clicking a ASP.Net button control

    Once the control is clicked the back end stores the users information (first name, last name, email address, phone number, etc…) to a database. After that information is stored to a database, the system would then allow the user to download the file through an generic handler (.ashx file). The file size that was to be downloaded is 600MB.

    Once the database is updated, I call the DownloadFile.ashx generic handler. Upon execution of the generic handler I received the following “OutOfMemoryException” error

    OutOfMemoryException error

    This was a result from the following generic handler I was using

    This handler worked in the past, for very small files. The last time I was using this the file size was about 300MB. My friend @homeraguas reminded me that I might needed to check my web.config file to be sure that the following maxRequestLength and executionTimeout was set. It wasn’t in the web.config file. So I added the following

    Recompiled and published, then gave it another go. Still, I came across the “OutOfMemoryException” error. I looked around the net and came across this blog’s article utilizing the following method Reponse.TransmitFile();

    The HttpReponse.TransmitFile() method basically states it “Writes the specified file directly to an HTTP response output stream without buffering it in memory.”

    This makes sense to me, since the file I want to transfer is 600MB and I do not think the current server I am writing this web application for does not have adequate resources available. So the revision to the code I wrote/used is as follows

    The result was that it worked.

    ashx generic handler worked

    What do you guys think? Is this an adequate solution? Have a good one!

  • 相关阅读:
    Spring---入门
    Struts2---数据的校验
    Mybatis入门(二)增删改查
    解决pyinstaller打包后运行,出现ModuleNotFoundError: No module named 'pywt._extensions._cwt'
    Python打包方法——Pyinstaller CentOS下踩坑记录
    Spring Boot + kkFileView-2.1.2 实现文档在线预览
    Spring Boot 文件下载
    Spring Boot 文件上传
    Spring Boot 整合 Shiro+Thymeleaf
    Spring Boot 整合 Druid && 配置数据源监控
  • 原文地址:https://www.cnblogs.com/rainbowzc/p/3611925.html
Copyright © 2020-2023  润新知