上传前限制图像文件大小(Limit image filesize before upload)

编程入门 行业动态 更新时间:2024-10-23 01:55:46
上传前限制图像文件大小(Limit image filesize before upload)

我通过AJAX将图像作为base64字符串上传到ASP.NET WebService。 由于AJAX / JSON,最大文件大小非常有限(从我的测试大约3MB之前,AJAX请求变得太长)。

那么有什么办法可以将给定图像的大小调整为FIXED文件大小(如3MB)而不是固定分辨率?

我现在已经谈到了用于调整大小的canvas的功能,但是设置分辨率和/或质量不仅仅是为了减少它。

我已经有了一个想法,就是让循环在几个周期内调整图像大小并通过检查base64长度来检查大致的文件大小,但这似乎既不高效又不够精确。

我知道我可以对请求进行分块,但我也必须更改WebService,我只想作为最后的手段。

I am uploading images as base64 strings to an ASP.NET WebService via AJAX. Because of AJAX/JSON, the maximum filesize is pretty limited (from my tests around 3MB before the AJAX requests get too long).

So is there any way, to resize a given image to a FIXED file size (like 3MB) and not a fixed resolution?

I already now about the capabilities of canvas for resizing purposes, but setting the resolution and/or quality isn't just cutting it for me.

An idea I already had, was to have a loop resize the image over a couple of cycles and check the approximate filesize by checking the base64 length, but that seems both not efficient and not precise enough.

I know I could chunk the requests, but I would have to change the WebService as well, which I only want to do as a last resort.

最满意答案

JSON文件的大小没有限制。 只要您有足够的资源来处理它,您就可以根据需要上传大文件。 4MB限制是ASP.NET端默认限制。

您可以将maxRequestLength属性添加到system.web - httpRuntime配置密钥,它将接受任何大小的文件:

<configuration> <system.web> <httpRuntime maxRequestLength = "1048576" /> </system.web> </configuration>

对于IIS 7+,您还需要添加:

<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>

如果您仍想调整图像大小,可以在服务器端或客户端执行此操作。 调整图像大小C# HTML5在上传之前预先调整图像大小

此外,还有一些插件可以执行此操作: 在上载到服务器之前使用javascript调整客户端大小的图像

服务器端方式更可靠且与浏览器无关,而客户端方法将减少到服务器的传入流量。

JSON file has no limits in its size. You can upload as large files as you need, as long as you have enough resources to handle it. The 4MB limit is ASP.NET-side default limit.

You can add a maxRequestLength attribute to your system.web - httpRuntime config key and it will accept files of any size:

<configuration> <system.web> <httpRuntime maxRequestLength = "1048576" /> </system.web> </configuration>

For IIS 7+ you will also need to add:

<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>

If you still want to resize an image, you can do this both server-side or client-side. Resize an Image C# HTML5 Pre-resize images before uploading

Also, there are some plugins to do this: Image resizing client-side with javascript before upload to the server

The server-side way is more reliable and browser-independent, while the client-side approach will decrease the amount of incoming traffic to server.

更多推荐

大小,AJAX,MB,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-08-06 22:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件大小   图像   上传   Limit   filesize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!