booody
booody

Reputation: 79

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

ex:-
   Uploaded file size is 1mb (or whatever it's size) --> reduce it to 500kb

   Any Help .... I will be grateful .

To be more Specific I want to upload only Images if this gonna help.

Upvotes: 1

Views: 2114

Answers (4)

Priyank
Priyank

Reputation: 10623

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.

Upvotes: 0

Dao
Dao

Reputation: 2824

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.

For that purpose you can use Silverlight, Flash. (Sure you cannot do it only with javascript because it has no access to client's file system). Here is example of implementing simple file upload using silverlight: Link. You will need to modify a bit code to cut file before upload.

Another variant is try to use Plupload. It allows you to upload files using HTML5 Gears, Silverlight, Flash providing ability of chunked uploads. 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.

Upvotes: 1

Ahmed Magdy
Ahmed Magdy

Reputation: 6030

You can increase the max. upload size by adding the following part to your web.config:

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

102400 == 1GB

Upvotes: 0

Jim
Jim

Reputation: 2146

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. If you use a tool like Fiddler http://www.fiddler2.com/fiddler2/, you can see it in there, Base64 Encoded, if I remember correctly. 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).

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.

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

Upvotes: 0

Related Questions