I have an image upload tool and I want to report an error before the file is uploaded if the size is too large for my server.
Is there a c# ASP .NET MVC 5 command for finding out this size?
I don't know if it makes a difference but i'm hosted with Microsoft Azure.
You server application can to know this value, by reading Web.config:
var section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
// Value in KBs. 4096 means 4Mb.
var maxRequestLength = section.MaxRequestLength;
You'd have to write a controller action that queried the IIS configuration and returned the value to the client.
The client would need to call this method to find out the limit before attempting the upload.
A way to find out the value is described here:
How to read maxAllowedContentLength
Related
I have a service that keeps failing with the above error. There are numerous posts and article's about the default size for file being 4mb and to increase the size via the web.config file. In my case the file size is 3.2mb so wouldn't expect to get this error. If I Increase the limit via the maxRequestLength attribute and the maxAllowedContentLength then this error is overcome. However, I'm trying to understand why I mighty be getting this error in the first place given that the filesize is below the 4mb limit. The file is an xml file and I'm posting to my service via postman.
I am trying to upload a file in ASP.net MVC and want to give a user a message that "You have exceeded maximum file size" if user exceeds the maximum file size but when I try to upload a large file it gives me Httpstatus code as 200 when i checked IIS logs there are 2 request logged in IIS one is HttpStatusCode 302 and another as 200.I am not doing any kind of redirect but IIS logs are giving me 302.How can I generate 404.13 error for these kind of request and how can I track this error so that I can give a meaningful message.
Using the HTML5 File API,
var size = document.getElementById('myFile').files[0].size;
Try this plugin Blueimp jquery file upload
u can see the plugin configuration here.it has an option of setting the max file size so that u can alert the user before a server round-trip.
I came across an issue today when adding an upload data modification to a ASP.NET 3.5 C# Web App. The user needs to upload a spreadsheet with a .xls extension containing large amounts of data. That spreadsheet will be converted to a datatable and then inserted into SQL Via a SqlBulkInsert method. The problem I am having is the sheet I am currently working with has 16 columns and can only contain 24889 rows before I receive this message from the server via Fiddler, 'ReadResponse() failed: The server did not return a response for this request.' I've searched and not found much info on this related to my issue. Any help would be appreciated.
Try giving some values for maxRequestLength like below
<system.web>
<!-- ... -->
<httpRuntime maxRequestLength="204800"/>
<!-- ... -->
</system.web>
By default you can upload a file upto 4MB.
The size of your excel must be excedding the limit.
What you can do is set the setting in Web Configuration file
What this will do is increase the request size that server can respond to.
You need to look into server logs to figure out what cases it (or simply debug the server portion if you can). You also should see if it cased by simply size of the file or data in the file (if you do any processing).
Possible reasons:
you code gets the data, but fails in some way likely tearing down whole process
ASP.Net blocks request due to size (see Pankaj Garg answer).
your code is just too slow
Is there a way to upload a file from local filesystem to a folder in a server using ASMX web services(no WCF, don't ask why:)?
UPD
P.S.file size can be 2-10 GB
Sure:
[WebMethod]
public void Upload(byte[] contents, string filename)
{
var appData = Server.MapPath("~/App_Data");
var file = Path.Combine(appData, Path.GetFileName(filename));
File.WriteAllBytes(file, contents);
}
then expose the service, generate a client proxy from the WSDL, invoke, standard stuff.
--
UPDATE:
I see your update now about handling large files. The MTOM protocol with streaming which is built into WCF is optimized for handling such scenarios.
When developing my free tool to upload large files to a server, I am also using .NET 2.0 and web services.
To make the application more error tolerant for very large files, I decided to not upload one large byte[] array but instead do a "chuncked" upload.
I.e. for uploading a 1 MB file, I do call my upload SOAP function 20 times, each call passing a byte[] array of 50 KB and concating it on the server together again.
I also count the packages, when one drops, I try to upload it again for several times.
This makes the upload more error tolerant and more responsive in the UI.
If you are interested, this is a CP article of the tool.
For very large files, the only efficient way to send them to web services is with MTOM. And MTOM is only supported in WCF, which you have ruled out. The only way to do this with old-style .asmx web services is the answer that #Darin Dimitrov gave. And with that solution, you'll have to suffer the cost of the file being base64 encoded (33% more bandwidth).
We had the same requirement, basically uploading a file via HTTP POST using the standard FileUpload controls on the client side.
In the end we just added an ASPX page to the ASMX web service project (after all its just a web project) - this allowed us to upload to i.e. http://foo/bar/Upload.aspx when the web service was at http://foo/bar/baz.asmx. This kept the functionality within the web service, even though it was using a separate web page.
This might or might not fit your requirements, #Darins approach would work as a workaround as well but you would have to make modifications on the client side for that, which wasn't an option for us.
You can try to convert the file to Base64 and pass it as a string to the service and then convert back to a byte array.
https://forums.asp.net/t/1980031.aspx?Web+Service+method+with+Byte+array+parameter+throws+ArgumentException
How to convert file to base64 in JavaScript?
The input is not a valid Base-64 string as it contains a non-base 64 character
I have a ASP .NET load balanced application (webservice and website). It runs on SQL server. I need to be able to provide large files for download. However, because of the load balancing situation, the files are stored in the SQL database as opposed to the file system. BITS seems to be the best approach. I have full control of the client. However, i don't know how to configure BITS to read the file from the database. I know how to write the C# code for that, but i don't know how to get BITS to hook into it as opposed to reading the file from the file system.
Any ideas?
You can create a custom http handler by implementing System.Web.IHttpHandler. The ProcessRequest(HttpContext context) method is where you will write your file retrieval code from the database. Since BITS operates with range requests you will need to parse the value of context.Request.Headers["Range"] to get the start and end bytes requested. In the ProcessRequest you can read the binary from the database using the SqlCommand.ExecuteReader(CommandBehavior.SequentialAccess) method and set the resulting binary in context.Response.OutputStream. Remember to call context.Response.Flush() at the end.
The custom HttpHandler will serve a particular file extension (e.g. '.file'). This is what needs to be done in IIS:
Both IIS Versions
Add to section in in web.config:
IIS 6.0
Add .file (application/x-zip-compressed) extension as MIME type for the website.
Add Application Extension (Website Properties Virtual Directory Configuration Mappings)
Extension: .file
Executable Path(s): %windir%\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll
IIS 7.0
Add to section in in web.config:
Add to section in in web.config:
<mimeMap fileExtension=".file" mimeType="application/x-zip-compressed" />
Hope that's enough to get you started.
Have a look at 2008 Books Online OpenSqlFilestream. That API has examples that may help you.