Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETSQL-Server
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

See my artical upload image with resize it also..
 

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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 895
1 OriginalGriff 523
2 thatraja 245
3 Abhinav S 243
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid