Hi irshadmpct,
Refer below code to upload large file in chunks.
C#
protected void Upload(object sender, EventArgs e)
{
string fileName = fuUpload.PostedFile.FileName;
int fileSizeInMB = 1;
string baseFileName = Path.GetFileName(fileName);
int bufferSize = fileSizeInMB * (1024 * 1024);
byte[] fsBuffer = new byte[1024];
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
int totalFileParts = 0;
if (fileStream.Length < bufferSize)
{
totalFileParts = 1;
}
else
{
float preciseFileParts = ((float)fileStream.Length / (float)bufferSize);
totalFileParts = (int)Math.Ceiling(preciseFileParts);
}
int filePartCount = 0;
while (fileStream.Position < fileStream.Length)
{
string filePartName = String.Format("{0}.part_{1}.{2}", baseFileName, (filePartCount + 1).ToString(), totalFileParts.ToString());
filePartName = Path.Combine(Server.MapPath("~/Uploads/"), filePartName);
using (FileStream fsFilePart = new FileStream(filePartName, FileMode.Create))
{
int bytesRemaining = bufferSize;
int bytesRead = 0;
while (bytesRemaining > 0 && (bytesRead = fileStream.Read(fsBuffer, 0, Math.Min(bytesRemaining, 1024))) > 0)
{
fsFilePart.Write(fsBuffer, 0, bytesRead);
bytesRemaining -= bytesRead;
}
}
filePartCount++;
}
}
string[] partFiles = Directory.GetFiles(Server.MapPath("~/Uploads/"));
using (FileStream fs = new FileStream(Server.MapPath("~/Uploads/Final/") + fuUpload.FileName, FileMode.Create))
{
foreach (string file in partFiles)
{
using (FileStream fileChunk = new FileStream(file, FileMode.Open))
{
fileChunk.CopyTo(fs);
}
File.Delete(file);
}
}
}
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = fuUpload.PostedFile.FileName
Dim fileSizeInMB As Integer = 1
Dim baseFileName As String = Path.GetFileName(fileName)
Dim bufferSize As Integer = fileSizeInMB * (1024 * 1024)
Dim fsBuffer As Byte() = New Byte(1023) {}
Using fileStream As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim totalFileParts As Integer = 0
If fileStream.Length < bufferSize Then
totalFileParts = 1
Else
Dim preciseFileParts As Single = (CSng(fileStream.Length) / CSng(bufferSize))
totalFileParts = CInt(Math.Ceiling(preciseFileParts))
End If
Dim filePartCount As Integer = 0
While fileStream.Position < fileStream.Length
Dim filePartName As String = String.Format("{0}.part_{1}.{2}", baseFileName, (filePartCount + 1).ToString(), totalFileParts.ToString())
filePartName = Path.Combine(Server.MapPath("~/Uploads/"), filePartName)
Using fsFilePart As FileStream = New FileStream(filePartName, FileMode.Create)
Dim bytesRemaining As Integer = bufferSize
Dim bytesRead As Integer = 0
While bytesRemaining > 0 AndAlso (bytesRead = fileStream.Read(fsBuffer, 0, Math.Min(bytesRemaining, 1024))) > 0 > 0
fsFilePart.Write(fsBuffer, 0, bytesRead)
bytesRemaining -= bytesRead
End While
End Using
filePartCount += 1
End While
End Using
Dim partFiles As String() = Directory.GetFiles(Server.MapPath("~/Uploads/"))
Using fs As FileStream = New FileStream(Server.MapPath("~/Uploads/Final/") + fuUpload.FileName, FileMode.Create)
For Each file As String In partFiles
Using fileChunk As FileStream = New FileStream(file, FileMode.Open)
fileChunk.CopyTo(fs)
End Using
System.IO.File.Delete(file)
Next
End Using
End Sub