ASP.NET Web Pages Not Responding to File Uploads

I have an ASP.NET webpage with a FileUpload control

<asp:FileUpload ID="fileUploadTN" style="width:250px" runat="server" /> 

One of the problems is that when uploading large files, my asp.net session ends. This is due to the restriction using DiscountAsp.net and an unoccupied 20-minute session timeout (if you do not switch to SqlServer). No problem downloading a large file, just a session, and then shortly after completion.

So, I have a workaround for the javascript timer to periodically access another webpage to keep the session longer.

  function timerEvent ()
        {
          jQuery.get ("/svc.aspx?act=ping", function (data) {});
        }

This works great except when downloading files. There is no HTTP GET response during file upload, and the request does not even get to the svc.aspx page. I can observe in Fiddler, and GET happens, but no answer. Once the file download is complete, the responses will begin again. But if the file is loading for a long time, my session may be disconnected.

Even if I open a new window in the browser and go directly to the svc.aspx page, it will get stuck in the request. I can open a window in another application browser window (e.g. FF -> Chrome) and respond with svc.aspx. Thus, it seems that the problem is in the same session that other HTTP GETs cannot respond during file download.

+4
source share
1 answer

For larger files, it is better to use some Flash downloader - for example Uploadify or SWFUpload. I prefer to use Uploadify. Below is an example of my project. You must include uploadify.css and jquery, swfobject and jquery.uploadify JavaScript files and initialize the upload on the ASPX page:

 <link href="uploadify.css" rel="stylesheet" type="text/css" /> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/swfobject.js" type="text/javascript"></script> <script src="Scripts/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script> <script type="text/javascript"> // <![CDATA[ $(document).ready(function () { $('#fileInput').uploadify({ 'uploader': 'Scripts/uploadify.swf', 'script': 'Upload.ashx', 'scriptData': {}, 'cancelImg': 'Images/cancel.png', 'auto': true, 'multi': true, 'fileDesc': 'PDF files', 'fileExt': '*.pdf', 'queueSizeLimit': 90, 'sizeLimit': 4000000, 'buttonText': 'Browse', 'folder': '/uploads', 'onComplete': function (event, queueID, fileObj, response, data) { var message = '<div>' + response + '</div>'; var results = $('#uploadResults'); results.html(message + results.html()); }, 'onAllComplete': function (event, queueID, fileObj, response, data) { } }); }); // ]]></script> 

Instead of asp: FileUpload control, use input with a file type. HTML is needed here:

  <input id="fileInput" name="fileInput" type="file" /> <div id="uploadResults"></div> 

Then create an ASHX handler that will handle you:

 public class Upload : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { try { HttpPostedFile file = context.Request.Files["Filedata"]; // Validate file for content length, extension etc. // Store file on server or do what you want with file // Use context.Response.Write to send messages which will be displayed in <div id="uploadResults"></div> } catch (Exception ex) { context.Response.Write("Failed to upload file"); } } } 
0
source

Source: https://habr.com/ru/post/1346764/


All Articles