简体   繁体   English

缩小上传的文件大小(图片)

[英]Shrink Uploaded file size (Image)

I Searched alot in the web but nothing ....... 我在网上搜索了很多,但是什么都没有。

My Problem is that I want to reduce the actual size of any uploaded file to a specific length . 我的问题是我想将任何上传文件的实际大小减小到特定长度

adnd I don't want to enforce the User to a specific ContentLength adnd我不想强制用户使用特定的ContentLength

ex:- 例如:-
Uploaded file size is 1mb (or whatever it's size) --> reduce it to 500kb 上传的文件大小为1mb(或任意大小)->减小到500kb

Any Help .... I will be grateful . 任何帮助..我将不胜感激。

To be more Specific I want to upload only Images if this gonna help . 更具体地说,我只想上传图片,如果这会有所帮助

So as I understand you want to allow user to upload file of any size but process on server only shrink file. 因此,据我了解,您希望允许用户上传任何大小的文件,但仅在服务器上处理收缩文件。 And don't waste time and traffic on uploading whole file. 并且不要在上传整个文件上浪费时间和流量。 So I don't know ability to interferer in process of receiving request body on IIS, that is why I propose to do shrinking on client side. 因此,我不知道是否有能力干扰IIS上接收请求正文的过程,这就是为什么我建议在客户端进行压缩。

For that purpose you can use Silverlight, Flash. 为此,您可以使用Silverlight,Flash。 (Sure you cannot do it only with javascript because it has no access to client's file system). (确保您不能仅使用javascript来执行此操作,因为它无法访问客户端的文件系统)。 Here is example of implementing simple file upload using silverlight: Link . 这是使用silverlight实现简单文件上传的示例: Link You will need to modify a bit code to cut file before upload. 上传之前,您需要修改位代码以剪切文件。

Another variant is try to use Plupload . 另一个变体是尝试使用Plupload It allows you to upload files using HTML5 Gears, Silverlight, Flash providing ability of chunked uploads. 它允许您使用HTML5 Gears,Silverlight,Flash上​​载文件,并提供分块上载的功能。 You can try to investigate this opensource project in order to adapt it to suit your needs. 您可以尝试研究此开源项目,以使其适应您的需求。

(You can also try to zip files on client side before sending) (您也可以尝试在发送之前在客户端上压缩文件)

Good luck. 祝好运。

If I understand your question correctly, I don't think that it is possible (or at least that easy). 如果我正确理解了您的问题,我认为不可能(或至少如此简单)。 When a user uploads a file to your web site, the file is actually POST data. 当用户将文件上传到您的网站时,该文件实际上是POST数据。 If you use a tool like Fiddler http://www.fiddler2.com/fiddler2/ , you can see it in there, Base64 Encoded, if I remember correctly. 如果您正确使用Fiddler http://www.fiddler2.com/fiddler2/之类的工具,则可以在其中看到Base64 Encoded。 So, the user has already sent the full data. 因此,用户已经发送了完整的数据。

I see you have tagged your question with asp.net , so, at this point asp.net is looking at the request, checks posting's length against the max size allowed (configured in web.config) and will reject the post if it is too long before you even are allowed to see it (I've tried). 我看到您已经用asp.net标记了您的问题,因此,此时,asp.net正在查看请求,并根据允许的最大大小(在web.config中配置)检查发布的长度,如果发布的长度过大,也会拒绝发布很久以前,您甚至还没有被允许看到它(我已经尝试过)。

Best I figure you can do (if this is what you really need to do) is set the maxRequestLength in your web config to a value large enough that will not get rejected, and then truncate the data yourself. 据我所知,您可以做的最好(如果这是您真正需要做的),是将Web配置中的maxRequestLength设置为一个不会被拒绝的足够大的值,然后自己截断数据。

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

You can increase the max. 您可以增加最大 upload size by adding the following part to your web.config : 通过将以下部分添加到您的web.config来上传大小:

<system.web>
   <httpRuntime  maxRequestLength="102400" executionTimeout="360"/>
</system.web>

102400 == 1GB 102400 == 1GB

Convert your file into byte array and split it into multiple byte arrays of your preferred size. 将文件转换为字节数组,然后将其拆分为所需大小的多个字节数组。 Later if you want, write those arrays into multiple different files. 以后,如果需要,可以将这些数组写入多个不同的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM