I am using VSTS 2008 + C# + .Net 3.5 + ASP.Net + IIS 7.0 to develop a console application at client side to upload a file, and at server side I receive this file using an aspx file.
From client side, I always notice (from console output) the upload percetage of the file increase from 1% to 50%, then to 100% suddenly. Any ideas what is wrong?
Here is my client side code,
class Program
{
private static WebClient client = new WebClient();
private static ManualResetEvent uploadLock = new ManualResetEvent(false);
private static void Upload()
{
try
{
Uri uri = new Uri("http://localhost/Default.aspx");
String filename = #"C:\test\1.dat";
client.Headers.Add("UserAgent", "TestAgent");
client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompleteCallback);
client.UploadFileAsync(uri, "POST", filename);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace.ToString());
}
}
public static void UploadFileCompleteCallback(object sender, UploadFileCompletedEventArgs e)
{
Console.WriteLine("Completed! ");
uploadLock.Set();
}
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
Console.WriteLine (e.ProgressPercentage);
}
static void Main(string[] args)
{
Upload();
uploadLock.WaitOne();
return;
}
}
Here is my server side code,
protected void Page_Load(object sender, EventArgs e)
{
string agent = HttpContext.Current.Request.Headers["UserAgent"];
using (FileStream file = new FileStream(#"C:\Test\Agent.txt", FileMode.Append, FileAccess.Write))
{
byte[] buf = Encoding.UTF8.GetBytes(agent);
file.Write(buf, 0, buf.Length);
}
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs("C:\\Test\\UploadFile.dat");
}
}
thanks in advance,
George
This is a known bug in the WebClient class. It will be fixed in .NET 4.0. Until then you could use HttpWebRequest to implement this functionality.
UPDATE: Here's an example of using synchronous HttpWebRequest to upload a file and track the progress:
public sealed class Uploader
{
public const int CHUNK_SIZE = 1024; // 1 KB
public void Upload(string url, string filename, Stream streamToUpload, Action<int> progress)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
string boundary = string.Format("---------------------{0}", DateTime.Now.Ticks.ToString("x"));
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.KeepAlive = true;
using (var requestStream = request.GetRequestStream())
{
var header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", boundary, filename);
var headerBytes = Encoding.ASCII.GetBytes(header);
requestStream.Write(headerBytes, 0, headerBytes.Length);
byte[] buffer = new byte[CHUNK_SIZE];
int bytesRead;
long total = streamToUpload.Length;
long totalBytesRead = 0;
while ((bytesRead = streamToUpload.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytesRead += bytesRead;
progress((int)(100 * totalBytesRead / total));
byte[] actual = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, actual, 0, bytesRead);
requestStream.Write(actual, 0, actual.Length);
}
}
using (var response = request.GetResponse()) { }
}
}
class Program
{
static void Main(string[] args)
{
var url = "http://localhost:2141/Default.aspx";
var filename = "1.dat";
var uploader = new Uploader();
using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
uploader.Upload(url, filename, fileStream, progress => Console.WriteLine("{0}% of \"{1}\" uploaded to {2}", progress, filename, url));
}
}
}
You can read webclient.UploadProgressChanged code.Then you will know the reason.Code below.
private void PostProgressChanged(AsyncOperation asyncOp, WebClient.ProgressData progress)
{
if (asyncOp == null || progress.BytesSent + progress.BytesReceived <= 0L)
return;
if (progress.HasUploadPhase)
{
int progressPercentage = progress.TotalBytesToReceive >= 0L || progress.BytesReceived != 0L
? (progress.TotalBytesToSend < 0L
? 50
: (progress.TotalBytesToReceive == 0L
? 100
: (int) (50L*progress.BytesReceived/progress.TotalBytesToReceive + 50L)))
: (progress.TotalBytesToSend < 0L
? 0
: (progress.TotalBytesToSend == 0L ? 50 : (int) (50L*progress.BytesSent/progress.TotalBytesToSend)));
asyncOp.Post(this.reportUploadProgressChanged,
(object)
new UploadProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState,
progress.BytesSent, progress.TotalBytesToSend, progress.BytesReceived,
progress.TotalBytesToReceive));
}
else
{
int progressPercentage = progress.TotalBytesToReceive < 0L
? 0
: (progress.TotalBytesToReceive == 0L
? 100
: (int) (100L*progress.BytesReceived/progress.TotalBytesToReceive));
asyncOp.Post(this.reportDownloadProgressChanged,
(object)
new DownloadProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState,
progress.BytesReceived, progress.TotalBytesToReceive));
}
}
This beheavior is by design, its 50% upload the file and 50% the response of the server.
Not a bug.
Related
I have created a Form Application to Extracted Image , I have searched many posts , till Now I am able to download into the MemoryStream (Byte Array ).
I am not able to save that byte array into file system and check the image size ....
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<string> FetchImages(string Url)
{
List<string> imageList = new List<string>();
if (!Url.StartsWith("http://") && !Url.StartsWith("https://"))
Url = "http://" + Url;
string responseUrl = string.Empty;
string htmlData = ASCIIEncoding.ASCII.GetString(DownloadData(Url, out responseUrl));
if (responseUrl != string.Empty)
Url = responseUrl;
if (htmlData != string.Empty)
{
string imageHtmlCode = "<img";
string imageSrcCode = #"src=""";
int index = htmlData.IndexOf(imageHtmlCode);
while (index != -1)
{
htmlData = htmlData.Substring(index);
int brackedEnd = htmlData.IndexOf('>'); //make sure data will be inside img tag
int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length;
int end = htmlData.IndexOf('"', start + 1);
if (end > start && start < brackedEnd)
{
string loc = htmlData.Substring(start, end - start);
imageList.Add(loc);
}
if (imageHtmlCode.Length < htmlData.Length)
index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length);
else
index = -1;
}
for (int i = 0; i < imageList.Count; i++)
{
string img = imageList[i];
string baseUrl = GetBaseURL(Url);
if ((!img.StartsWith("http://") && !img.StartsWith("https://"))
&& baseUrl != string.Empty)
img = baseUrl + "/" + img.TrimStart('/');
imageList[i] = img;
}
}
return imageList;
}
private byte[] DownloadData(string Url)
{
string empty = string.Empty;
return DownloadData(Url, out empty);
}
private byte[] DownloadData(string Url, out string responseUrl)
{
byte[] downloadedData = new byte[0];
try
{
WebRequest req = WebRequest.Create(Url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
responseUrl = response.ResponseUri.ToString();
byte[] buffer = new byte[1024];
MemoryStream memStream = new MemoryStream();
while (true)
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
memStream.Write(buffer, 0, bytesRead);
}
}
downloadedData = memStream.ToArray();
stream.Close();
memStream.Close();
}
catch (Exception)
{
responseUrl = string.Empty;
return new byte[0];
}
return downloadedData;
}
private Image ImageFromURL(string Url)
{
byte[] imageData = DownloadData(Url);
Image img = null;
try
{
MemoryStream stream = new MemoryStream(imageData);
img = Image.FromStream(stream);
stream.Close();
}
catch (Exception)
{
}
return img;
}
private string GetBaseURL(string Url)
{
int inx = Url.IndexOf("://") + "://".Length;
int end = Url.IndexOf('/', inx);
string baseUrl = string.Empty;
if (end != -1)
return Url.Substring(0, end);
else
return string.Empty;
}
private void btnGetImages_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
listImages.Items.Clear();
foreach (string image in FetchImages(txtURL.Text))
{
listImages.Items.Add(image);
}
this.Cursor = Cursors.Default;
}
private void btnView_Click(object sender, EventArgs e)
{
if (listImages.SelectedIndex != -1)
picImage.Image = ImageFromURL(listImages.SelectedItem.ToString());
}
private void btnSave_Click(object sender, EventArgs e)
{
}
private void btnDownload_Click(object sender, EventArgs e)
{
DownloadData(txtURL.Text);
}
.. I am trying to save the memroy stream into hard drive but still not getting exact code ,
i think this code needs some additional changes
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
}
}
Any Help will be appreciated
If you want to save the Byte[] array to a file,
Syntax : File.WriteAllBytes(string path, byte[] bytes)
Eg : File.WriteAllBytes("Foo.txt", arrBytes); // Requires System.IO
refer this link for more info http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx
If you want to convert the Byte back to image and save to the drive.
Eg:
byte[] bitmap = YourImage();
using(Image image = Image.FromStream(new MemoryStream(bitmap)))
{
image.Save("image.jpg", ImageFormat.Jpeg);
}
I have made a download program in C#. It is a queue downloader.
You can see how it works here: Click Express Downloader
Is there any faster method to download?
Here is the method i use, it must support resume support.
private void Download(object startPoint)
{
try
{
try
{
//int startPointInt = Convert.ToInt32(startPoint);
Int64 startPointInt = Convert.ToInt64(startPoint);
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.AddRange(startPointInt);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse)webRequest.GetResponse();
Int64 fileSize = webResponse.ContentLength;
strResponse = webResponse.GetResponseStream();
if (startPointInt == 0)
{
strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Create, FileAccess.Write, FileShare.None);
}
else
{
strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Append, FileAccess.Write, FileShare.None);
}
int bytesSize = 0;
byte[] downBuffer = new byte[4096];
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });
if (goPause == true)
{
break;
}
}
}
catch { }
}
finally
{
strResponse.Close();
strLocal.Close();
}
}
You have to do two things,
Get faster bandwidth
Change block size from 4096 to something like 8192 or any number which can be hold in memory easily.
The idea is that if I selected more than one file then upload one file when, then when it finishes to upload then upload the next one and so on.
I changed the filedialoug to be able to select multi:
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
this.txtUploadFile.Text = this.openFileDialog1.FileName;
}
I can select more than one file the problem is that when using a breakpoint I see on this.txtUploadFile.Text one file only the first I selected. txtUploadFile is a textBox.
The first thing is: how can I see on the textBox(txtUploadFile) all the selected files and not only one? Or how can I get some indication that all the files I selected really selected? Maybe to show somehow in the textBox something that all files selected?
The important part is: how do I upload all the selected files one by one?
This is the button click event that starting the files uploading:
private void btnUpload_Click(object sender, EventArgs e)
{
if(this.ftpProgress1.IsBusy)
{
this.ftpProgress1.CancelAsync();
this.btnUpload.Text = "Upload";
}
else
{
FtpSettings f = new FtpSettings();
f.Host = this.txtHost.Text;
f.Username = this.txtUsername.Text;
f.Password = this.txtPassword.Text;
f.TargetFolder = this.txtDir.Text;
f.SourceFile = this.txtUploadFile.Text;
f.Passive = this.chkPassive.Checked;
try
{
f.Port = Int32.Parse(this.txtPort.Text);
}
catch { }
this.toolStripProgressBar1.Visible = true;
this.ftpProgress1.RunWorkerAsync(f);
this.btnUpload.Text = "Cancel";
}
}
I also have a backgroundworker with a progressBar so each file that is uploading the progressBar should show the progress of the uploading today it's showing for one file since I can upload only one file:
private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripStatusLabel1.Text = e.UserState.ToString(); // The message will be something like: 45 Kb / 102.12 Mb
this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);
}
private void ftpProgress1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Error != null)
MessageBox.Show(e.Error.ToString(), "FTP error");
else if(e.Cancelled)
this.toolStripStatusLabel1.Text = "Upload Cancelled";
else
this.toolStripStatusLabel1.Text = "Upload Complete";
this.btnUpload.Text = "Upload";
this.toolStripProgressBar1.Visible = true;
}
And last the class of the FTP uploader:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace FTP_ProgressBar
{
public partial class FtpProgress : BackgroundWorker
{
public static string ConnectionError;
private FtpSettings f;
public FtpProgress()
{
InitializeComponent();
}
public FtpProgress(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
try
{
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(f.SourceFile));
if (!UploadPath.ToLower().StartsWith("ftp://"))
UploadPath = "ftp://" + UploadPath;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
request.UseBinary = true;
request.UsePassive = f.Passive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.Credentials = new NetworkCredential(f.Username, f.Password);
long FileSize = new FileInfo(f.SourceFile).Length;
string FileSizeDescription = GetFileSize(FileSize);
int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.Open(f.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int BytesRead = fs.Read(Buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;
requestStream.Write(Buffer, 0, BytesRead);
SentBytes += BytesRead;
string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
ConnectionError = "Error: Please check the ftp address";
break;
case WebExceptionStatus.Timeout:
ConnectionError = "Error: Timout Request";
break;
}
}
}
public static string GetFileSize(long numBytes)
{
string fileSize = "";
if(numBytes > 1073741824)
fileSize = String.Format("{0:0.00} Gb", (double)numBytes / 1073741824);
else if(numBytes > 1048576)
fileSize = String.Format("{0:0.00} Mb", (double)numBytes / 1048576);
else
fileSize = String.Format("{0:0} Kb", (double)numBytes / 1024);
if(fileSize == "0 Kb")
fileSize = "1 Kb";
return fileSize;
}
}
public class FtpSettings
{
public string Host, Username, Password, TargetFolder, SourceFile;
public bool Passive;
public int Port = 21;
}
}
It is a bit long, but I couldn't find a way to narrow the code since it's all connected to each other.
What I need it to do is if I selected more than one file make something like a queue and automatic upload one file when finished upload the next file and so on until the last file.
Today I can upload only one file. Single file each time.
I need that it will automatically upload all the selected files. Using the progressBar and the backgroundworker like it is now.
Here is a procedure how to upload multiple files from the directory where ftp - an address of ftp server:
private void uploadFTP(DirectoryInfo d, string ftp)
{
FileInfo[] flist = d.GetFiles();
if (flist.GetLength(0) > 0)
{
foreach (FileInfo txf in flist)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftp + txf.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FileStream stream = File.OpenRead(txf.FullName);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
txf.Delete();
}
}
}
You should be using
OpenFileDialog1.FileNames
And iterate on those elements to upload them one by one.
FileNames Property
I'm using those methods to upload files,
Client Side:
private void Add(object sender, MouseButtonEventArgs e)
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.Multiselect = true;
OFD.Filter = "Python (*.py)|*.py";
bool? Result = OFD.ShowDialog();
if (Result != null && Result == true)
foreach (var File in OFD.Files)
mylistbox.Items.Add(File);
}
private void Submit(object sender, MouseButtonEventArgs e)
{
foreach (var File in mylistbox.Items)
Process(((FileInfo)File).Name.Replace(((FileInfo)File).Extension, string.Empty), ((FileInfo)File).OpenRead());
}
private void Process(string File, Stream Data)
{
UriBuilder Endpoint = new UriBuilder("http://localhost:5180/Endpoint.ashx");
Endpoint.Query = string.Format("File={0}", File);
WebClient Client = new WebClient();
Client.OpenWriteCompleted += (sender, e) =>
{
Send(Data, e.Result);
e.Result.Close();
Data.Close();
};
Client.OpenWriteAsync(Endpoint.Uri);
}
private void Send(Stream Input, Stream Output)
{
byte[] Buffer = new byte[4096];
int Flag;
while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0)
{
Output.Write(Buffer, 0, Flag);
}
}
Server Side:
public void ProcessRequest(HttpContext Context)
{
using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py")))
{
Save(Context.Request.InputStream, Stream);
}
}
private void Save(Stream Input, FileStream Output)
{
byte[] Buffer = new byte[4096];
int Flag;
while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0)
{
Output.Write(Buffer, 0, Flag);
}
}
My problem is that uploaded files have different Creation, Modification & Access Dates.
Why ?
When you upload a file, you are essentially creating a new file, with duplicated contents.
From your code:
using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py")))
{
Save(Context.Request.InputStream, Stream);
}
The File.Create is responsible for the new dates.
See this answer for information on preserving the dates.
I am developing an application for windows phone 7.From here I want to upload an image file to the remote server.I am using the following code to receive the file at my receiving end:
if (Request.Files.Count > 0)
{
string UserName = Request.QueryString["SomeString"].ToString();
HttpFileCollection MyFilecollection = Request.Files;
string ImageName = System.Guid.NewGuid().ToString() + MyFilecollection[0].FileName;
MyFilecollection[0].SaveAs(Server.MapPath("~/Images/" + ImageName));
}
Now my problem is, how can I post the file from my windows phone 7 platform (using PhotoChooserTask).I have tried the following code but with no success.(the following code is called from the PhotoChooserTask completed event handler.
private void UploadFile(string fileName, Stream data)
{
char[] ch=new char[1];
ch[0] = '\\';
string [] flname=fileName.Split(ch);
UriBuilder ub = new UriBuilder("http://www.Mywebsite.com?SomeString="+ussi );
ub.Query = string.Format("name={0}", flname[6]);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
Please help me out to get out from this problem.
Thanks
I was able to get this working, but not using the Files collection. From my post at http://chriskoenig.net/2011/08/19/upload-files-from-windows-phone/:
Client Code
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void SelectButton_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask task = new PhotoChooserTask();
task.Completed += task_Completed;
task.Show();
}
private void task_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK)
return;
const int BLOCK_SIZE = 4096;
Uri uri = new Uri("http://localhost:4223/File/Upload", UriKind.Absolute);
WebClient wc = new WebClient();
wc.AllowReadStreamBuffering = true;
wc.AllowWriteStreamBuffering = true;
// what to do when write stream is open
wc.OpenWriteCompleted += (s, args) =>
{
using (BinaryReader br = new BinaryReader(e.ChosenPhoto))
{
using (BinaryWriter bw = new BinaryWriter(args.Result))
{
long bCount = 0;
long fileSize = e.ChosenPhoto.Length;
byte[] bytes = new byte[BLOCK_SIZE];
do
{
bytes = br.ReadBytes(BLOCK_SIZE);
bCount += bytes.Length;
bw.Write(bytes);
} while (bCount < fileSize);
}
}
};
// what to do when writing is complete
wc.WriteStreamClosed += (s, args) =>
{
MessageBox.Show("Send Complete");
};
// Write to the WebClient
wc.OpenWriteAsync(uri, "POST");
}
}
Server Code
public class FileController : Controller
{
[HttpPost]
public ActionResult Upload()
{
string filename = Server.MapPath("/Uploads/" + Path.GetRandomFileName();
try
{
using (FileStream fs = new FileStream(filename), FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
using (BinaryReader br = new BinaryReader(Request.InputStream))
{
long bCount = 0;
long fileSize = br.BaseStream.Length;
const int BLOCK_SIZE = 4096;
byte[] bytes = new byte[BLOCK_SIZE];
do
{
bytes = br.ReadBytes(BLOCK_SIZE);
bCount += bytes.Length;
bw.Write(bytes);
} while (bCount < fileSize);
}
}
}
return Json(new { Result = "Complete" });
}
catch (Exception ex)
{
return Json(new { Result = "Error", Message = ex.Message });
}
}
}
Note that I'm using ASP.NET MVC to receive my file, but you should be able to use the same, core logic with WebForms.
/chris