-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadEngine.aspx.cs
68 lines (64 loc) · 3.15 KB
/
UploadEngine.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class UploadEngine : System.Web.UI.Page
{
#region Web Methods
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
UploadDetail Upload = (UploadDetail)this.Session["UploadDetail"];
//Let the webservie know that we are not yet ready
Upload.IsReady = false;
if (this.fileUpload.PostedFile != null && this.fileUpload.PostedFile.ContentLength > 0)
{
//build the local path where upload all the files
string path = this.Server.MapPath(@"Uploads");
string fileName = Path.GetFileName(this.fileUpload.PostedFile.FileName);
//Build the strucutre and stuff it into session
Upload.ContentLength = this.fileUpload.PostedFile.ContentLength;
Upload.FileName = fileName;
Upload.UploadedLength = 0;
//Let the polling process know that we are done initializing ...
Upload.IsReady = true;
//set the buffer size to something larger.
//the smaller the buffer the longer it will take to download,
//but the more precise your progress bar will be.
int bufferSize = 1;
byte[] buffer = new byte[bufferSize];
//Writing the byte to disk
using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
//Aslong was we haven't written everything ...
while (Upload.UploadedLength < Upload.ContentLength)
{
//Fill the buffer from the input stream
int bytes = this.fileUpload.PostedFile.InputStream.Read(buffer, 0, bufferSize);
//Writing the bytes to the file stream
fs.Write(buffer, 0, bytes);
//Update the number the webservice is polling on to the session
Upload.UploadedLength += bytes;
}
}
//Call parent page know we have processed the uplaod
const string js = "window.parent.onComplete(1,'File uploaded successfully.','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", string.Format(js, fileName, Upload.UploadedLength, Upload.ContentLength), true);
}
else
{
//Call parent page know we have processed the uplaod
const string js = "window.parent.onComplete(4, 'There was a problem with the file.','','0 of 0 Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", js, true);
}
//Let webservie know that we are not yet ready
Upload.IsReady = false;
}
}
#endregion
}