0

I am using this URL to select a file from a local PC: https://asp.net-tutorials.com/controls/file-upload-control/

If I select a file which is about 17 characters long with 3 character extension, I get error:

HTTP Error 404.13 - Not Found
The request filtering module is configured to deny a request that exceeds the request content length.

Example of the file is My123LongFileName.mp3

If I select a different file, smaller (e.g. File123.mp3), no error.

How do I allow my ASP.NET app to select long name files?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Hidalgo
  • 941
  • 2
  • 14
  • 38
  • 3
    Are sure the problem is not the size of the file itself, rather than the length of the name? The error is about the content of the whole request, which would include the file data, if you're doing file uploads. Restricting a HTTP upload to a dozen characters of data (or whatever) would be excessively restrictive, and prevent you from submitting pretty much anything useful at all from a form. – ADyson Nov 23 '22 at 14:34
  • @ADyson Thank you for your prompt reply. I probably misunderstand how this control works. The error happens on line: string filename = System.IO.Path.GetFileName(FileUploadControl.FileName); So, you are saying that the above line gets the entire file not just the name. I need just the name the local path to the file. Is it possible? – Hidalgo Nov 23 '22 at 14:40
  • 1
    In the background it's possible that it does, in order to try and pull the data. Although are you sure it comes from that line? 404 is actually a HTTP "Not Found" error, it would likely hit that before getting to any C# code. I think the error actually originates in IIS though - see https://stackoverflow.com/questions/10871881/iis7-the-request-filtering-module-is-configured-to-deny-a-request-that-exceeds (and others, if you google the error message). It's likely you need to adjust the value in your web.config to change the max allowed upload size. – ADyson Nov 23 '22 at 14:53
  • @ADyson The property .FileName is misleading since it shows the file name and yet it has the content of the file. How do I get in the background (as you say) the file name (not the content) in the FileUploadControl? The control has both the Browse button and some kind of a text box. How do you get the only name of the selected file from the "text box"? – Hidalgo Nov 23 '22 at 14:57
  • 1
    According to the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.fileupload.filename?view=netframework-4.8) it gets the filename, there are other properties listed there to get the file contents if needed. – ADyson Nov 23 '22 at 15:00
  • But regardless, the solution to your issue is likely to be as per the stackoverflow link I provided above. – ADyson Nov 23 '22 at 15:00
  • @ADyson Thank you. Here is what I don't understand: 1. The file is selected and the name is shown in the FileuploadControl text box. 2 I placed a dummy button on the form - a button that has not method. And when I click on it, the error pops up. Why? – Hidalgo Nov 23 '22 at 15:14
  • 1
    Because even if you didn't specify a C# callback method for the button, if it's a submit button then it still submits the form when you press it (all WebForms pages contain a HTML form). That causes the browser to send the form data to the server (including the selected file). The data reaches the server, and passes to IIS. IIS looks at it and says "that request is too big", and returns the 404.13 error. Like I said before, the request almost certainly never gets to ASP.NET or C# - IIS will give the error before it even passes the request on to the ASP.NET handler. – ADyson Nov 23 '22 at 15:17
  • https://www.west-wind.com/presentations/howaspnetworks/Figure%207.png - on this diagram, your request isn't getting past the IIS stage at the bottom (which is the first step). – ADyson Nov 23 '22 at 15:26
  • Thank you very much. Now I understand. I need to change my approach to using HTML Input control – Hidalgo Nov 23 '22 at 15:28
  • Nothing happens until you post-back the page (by even any button on the page). THEN and ONLY then after the page is posted (sent to server) can code behind run. At that point, the file upload control will have the file name (and ONLY the file name - path names on client-side computer are protected and not your business to get/know about). So for security reasons, browsers ONLY give/send you the file name - not path name. File is not up-loaded nor can code behind do anything such as get file name until that postback. it is possible to send/upload ONLY the file name(s) selected with js – Albert D. Kallal Nov 23 '22 at 15:29
  • `I need to change my approach to using HTML Input control`...no, you just need to increase the size of request that IIS permits, as I mentioned earlier. It doesn't matter what kind of control you use... if you upload a file that's too big then IIS will reject it. – ADyson Nov 23 '22 at 15:31
  • Yes, the URL you posted probably will solve the issue. But in the long run I need to save the file to a table (SQL Server table). And I don't know how to store the file content (in a string) to a SQL Sever table. The customer may select a JPEG file. And I don't know if this type of file can be converted to a string. Again, thank you. – Hidalgo Nov 23 '22 at 15:36
  • 1
    `I don't know how to store the file content (in a string) to a SQL Sever table`...well that's a separate issue which you can almost certainly find examples of already online, it's a common thing. e.g. this one: https://www.c-sharpcorner.com/article/upload-files-and-save-into-database-in-binary-format-using-asp-net/ (I just googled "asp.net webforms upload file and save to sql server"). P.S. If it's a binary file such as mp3 then saving it as a string makes no sense, you should save it as binary. Even if the user selects a text file you can still store it like that, with no harm done. – ADyson Nov 23 '22 at 15:43
  • 1
    P.P.S. It seems like you might be new to web programming, and perhaps programming in general. If so, then why on earth are you learning WebForms? It's a legacy technology from 20 years ago, no-one with any sense is writing anything new with it anymore. The only reason to learn it would be if you take a job maintaining some old software. You'd be far better to learn the modern variants of ASP.NET such as Blazor or Razor Pages. – ADyson Nov 23 '22 at 15:46
  • Because I developed this ASP.NET app about 20 years ago. I still maintain it and sometimes need to add a feature. – Hidalgo Nov 23 '22 at 15:48
  • 1
    Ok I see. From your writing and questions it seems like you never coded anything like this before. Maybe you just forgot all the things you learned 20 years ago? :-) – ADyson Nov 23 '22 at 15:49
  • I started using WebForms as my first venture into .NET (and web programming in general), when .NET 2.0 was just released. It was horrible and I'm very glad I moved onto MVC after a a few years, and then onto newer things after that. Even MVC seems relatively clunky now compared to the newer technologies. Unless it's a very big application which would take too long to update, it might benefit from time spent on a rewrite. :-) – ADyson Nov 23 '22 at 16:06
  • Right now, my challenge is to re-write the front of the application, to make it responsive. Using Bootstrap. – Hidalgo Nov 23 '22 at 16:11
  • That will be a lot easier if you're not constrained by WebForms's built-in server-side controls :-) – ADyson Nov 23 '22 at 16:18
  • I am sure you know what you are talking about. You are right. – Hidalgo Nov 23 '22 at 16:23
  • making a web site "responsive" has quite much ZERO to do with having used webforms. The term "responsive" means it plays nice with different sized screens - that's bootstrap and has quite much zero to do with having used webforms or system "x" here. All webform output is standard html, and as a result adding css and things like bootstrap layouts has next to zero with the goal of building responsive web sites. Browsers don't care about web forms anymore then hand coding html or having used PHP, or asp.net webforms. – Albert D. Kallal Nov 23 '22 at 20:20

1 Answers1

2

I think and suggest in this day and age, if you looking to setup some kind of file-uploading page and system?

Use a ajax enabled file up-loader.

They are great, since they up-load in "small chunks". This not only allows one to up-load files of really any size, but also means that the user gets a nice progress bar during up-load (so for a larger file it don't look like the browser has frozen). Even better, is they also allow a graceful cancel of such up-loads and respond "instant" to a user deciding to cancel.

There are a good number of file up-loaders free for the taking.

For example, I use the ajaxtoolkit up-loader, and it looks like this:

enter image description here

So, it allows multiple files, has a progress bar. And even has a "hot spot" in which you can just drag + drop files onto the web page.

So, during a up-load you have a progress bar (and cancel upload).

Looks like this:

enter image description here

It also has a rather "nice" server-side event model.

(on startup-load, on-upload one file done, on all done).

So, after up-loading, then I can display the files like this:

enter image description here

So, I don't think it worth to "roll" your own file up-loader, and as noted, there are many sample up-loaders you can adopt - many are free like the above one (ajaxfileUpload control) from the ajaxtoolkit for webforms).

As noted, since this up-loader uses "small" chunks for the file, then up-load size is quite much un-limited in size, and you don't overload the browser and server attempting to push up some huge "one big file" in a single post-back. In fact, the above up-loader runs and does not DO ANY post-backs.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51