FileUpload Control

Posted by Itsmaninderkaur under ASP.NET on 12/5/2009 | Views : 2161 | Status : [Member] | Replies : 2
How to upload an image in C# using guid and having only few extensions like " .jpg and .gif"




Responses

Posted by: Abhijit Jana on: 12/5/2009 [Member] [MVP] Bronze

Up
0
Down
If you are uploading file using File upload control then you can use SaveAs() method to save the file with GUID. create GUID First and then same it with FileUploadContr.SaveAS() method.
And how to restrict file type : check this
http://forums.asp.net/p/1051895/2171502.aspx

Cheers !
Abhijit

Itsmaninderkaur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/5/2009 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Uploading a file in the server is very easy. Just you will find in the files collection of every request object when you have placed one Input type=file control in the page.

In Web browser there is no option to specify the mime type unless you use some external application (flash/silverlight) to choose file. Thus you cannot have filter option for "jpg and gif" only while choosing the file. Rather you can check the mime type in the server to determine the actual file types.

You might use <asp:FileUpload to do this very easily.

Place :
<asp:FileUpload runat="server" ID="fupload"/>


Now in the server side :

if(fupload.PostedFile.ContentType.equals("image/jpeg") || fupload.PostedFile.ContentType.equals("image/gif") )

fupload.SaveAs(new Guid().ToString() + ".gif");


Thus by this way you can save the image in the server.

Change the logic around this code to suit your needs.
Cheers.

www.abhisheksur.com

Itsmaninderkaur, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response