Click here to Skip to main content
12,943,299 members (62,141 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello Sir/Madam
I need the coding for uploading images in asp .net.
Posted 8-Mar-10 17:53pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
<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.
myFile.PostedFile.SaveAs("DestinationPath");


Code:

<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>
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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);
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

In asp.net files are uploaded to the server using FileUpload Control.
MSDN: How to: Upload Files with the FileUpload Web Server Control[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Take a File Upload Control:

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



Code Behind:

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.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,930
Maciej Los 2,328
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100