Hi nauna,
Check this sample. Now take its reference and correct your code.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" multiple="multiple" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="OnUpload" /><br />
Class File
C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Web;
public class UploadClass
{
public string UploadImages(HttpPostedFile postedFile)
{
string uploadFileName = "";
string ext = Path.GetExtension(postedFile.FileName).ToLower();
if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png" || ext == ".pdf" ||
ext == ".docx" || ext == ".pptx" || ext == ".xlsx" || ext == ".doc" || ext == ".xls" || ext == ".ppt")
{
uploadFileName = Guid.NewGuid().ToString() + ext;
}
string ftp = ftppath;
string ftpFolder = domainname + "/DynamicImage/" + uploadFileName;
string contentType = postedFile.ContentType;
byte[] fileBytes = null;
string fileName = Path.GetFileName(uploadFileName);
postedFile.InputStream.Seek(0, SeekOrigin.Begin);
using (var binaryReader = new BinaryReader(postedFile.InputStream))
{
fileBytes = binaryReader.ReadBytes(postedFile.ContentLength);
}
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblImage VALUES(@FileName,@ContentType,@Data)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", fileBytes);
}
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
path = returnpath + "/DynamicImage/" + uploadFileName;
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
return path;
}
}
VB.Net
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Imports System.Web
Public Class UploadClass
Public Function UploadImages(ByVal postedFile As HttpPostedFile) As String
Dim uploadFileName As String = ""
Dim ext As String = Path.GetExtension(postedFile.FileName).ToLower()
If ext = ".jpg" OrElse ext = ".jpeg" OrElse ext = ".gif" OrElse ext = ".png" OrElse ext = ".pdf" OrElse ext = ".docx" _
OrElse ext = ".pptx" OrElse ext = ".xlsx" OrElse ext = ".doc" OrElse ext = ".xls" OrElse ext = ".ppt" Then
uploadFileName = Guid.NewGuid().ToString() & ext
End If
Dim ftp As String = ftppath
Dim ftpFolder As String = domainname & "/DynamicImage/" & uploadFileName
Dim contentType As String = postedFile.ContentType
Dim fileBytes As Byte() = Nothing
Dim fileName As String = Path.GetFileName(uploadFileName)
postedFile.InputStream.Seek(0, SeekOrigin.Begin)
Using binaryReader = New BinaryReader(postedFile.InputStream)
fileBytes = binaryReader.ReadBytes(postedFile.ContentLength)
End Using
Try
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblImage VALUES(@FileName,@ContentType,@Data)")
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@FileName", fileName)
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", fileBytes)
End Using
End Using
Dim request As FtpWebRequest = CType(WebRequest.Create(ftp & ftpFolder), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(username, password)
request.ContentLength = fileBytes.Length
request.UsePassive = True
request.UseBinary = True
request.ServicePoint.ConnectionLimit = fileBytes.Length
request.EnableSsl = False
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(fileBytes, 0, fileBytes.Length)
requestStream.Close()
End Using
Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
path = returnpath & "/DynamicImage/" & uploadFileName
response.Close()
Catch ex As WebException
Throw New Exception((TryCast(ex.Response, FtpWebResponse)).StatusDescription)
End Try
Return path
End Function
End Class
Code
C#
protected void OnUpload(object sender, EventArgs e)
{
if (FileUpload1.HasFiles)
{
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
Image1.ImageUrl = UploadImages(postedFile);
}
}
}
VB.Net
Protected Sub OnUpload(ByVal sender As Object, ByVal e As EventArgs)
If FileUpload1.HasFiles Then
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
Image1.ImageUrl = UploadImages(postedFile)
Next
End If
End Sub