Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir/Madam
I need the coding for uploading images in asp .net.
Posted

 
Share this answer
 
First of all, we need a HTML server control to allow the user to select the file. This is nothing but the same old input tag, with the type set to File, such as
XML
<input type=file id=myFile runat=server />
This will give you the textbox and a browse button. Once you have this, the user can select any file from their computer (or even from a network). Then, in the Server side, we need the following line to save the file to the Web Server.
C#
myFile.PostedFile.SaveAs("DestinationPath");


Code:

XML
<html>
<head>
<script language="VB" runat="server">
Sub Upload(Source As Object, e As EventArgs)
   If Not (myFile.PostedFile Is Nothing) Then
      Dim intFileNameLength as Integer
      Dim strFileNamePath as String
      Dim strFileNameOnly as String
      'Logic to find the FileName (excluding the path)
      strFileNamePath = MyFile.PostedFile.FileName
      intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
      strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
      myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
      lblMsg.Text = "File Upload Success."
   End If
End Sub
</script>
</head>
<body>
<h3>File Upload</h3>
<form enctype="multipart/form-data" runat="server">
   File: <input id="myFile" type="file" runat="server">
   <asp:label id=lblMsg runat="server" />
   <input type=button value="Upload" OnServerClick="Upload" runat="server">
</form>
</body>
</html>
 
Share this answer
 
v2
C#
if (imageUpload.PostedFile.ContentLength == 0)
       {
           lblImage.Visible = true;
           lblImage.Text = "Please upload an Image";
           return;
       }
      string strpath =  Server.MapPath("Upload/" + imageUpload.FileName);
      imageUpload.SaveAs(strpath);
 
Share this answer
 
v2
In asp.net files are uploaded to the server using FileUpload Control.
MSDN: How to: Upload Files with the FileUpload Web Server Control[^]
 
Share this answer
 
Take a File Upload Control:

<asp:FileUpload ID="fuSpeechFile" TabIndex="10" runat="server" Width="500px" />



Code Behind:

C#
if (fuSpeechFile.HasFile == true)
{
if (fuSpeechFile.PostedFile.FileName.ToLower().Contains(".jpg") || fuSpeechFile.PostedFile.FileName.ToLower().Contains(".gif") || fuSpeechFile.PostedFile.FileName.ToLower().Contains(".jpeg") || fuSpeechFile.PostedFile.FileName.ToLower().Contains(".bmp") || fuSpeechFile.PostedFile.FileName.ToLower().Contains(".png"))
{
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromStream(fuSpeechFile.PostedFile.InputStream);
img = ImageCompress.resizeImage(img, new System.Drawing.Size(Math.Min(img.Width, 1024), Math.Min(img.Height, 768)));
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] picbyte = memoryStream.ToArray();
objPhaseActivation.SpeechFile = picbyte;// get bytes
string ext = System.IO.Path.GetExtension(fuSpeechFile.PostedFile.FileName);
objPhaseActivation.FileExtension = ext; // get extension
}
catch (Exception ex)
{
lblWarning.Text = ex.Message;
return;
}
finally
{
	memoryStream.Close();
}
}
}
}


Minal Shah.
 
Share this answer
 
v2
See my artical upload image with resize it also..


http://hemantrautela.blogspot.in/2012/03/image-resize-in-aspnet-with-cnet.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900