?? instance.cs
字號:
using System;
using System.Configuration;
using System.Web;
namespace BufferedUpload
{
/// <summary>
/// The chunked file upload is session-based and the Instance class acts as a manager for the upload.
/// The object is fetched from session state for each method used in the upload.
/// </summary>
public sealed class Instance
{
private long bytesReceived = 0;
private string instanceId = "";
public string tempFilename = "";
private string targetFolder = "Uploads";
public long BytesReceived { get { return this.bytesReceived; } set { this.bytesReceived = value; } }
public string Id { get { return this.instanceId; } }
public string TargetFolder { get { return this.targetFolder; } set { this.TargetFolder = value; } }
public Instance() { }
public Instance(string Filename, bool Overwrite)
{
this.tempFilename = HttpContext.Current.Server.MapPath(this.targetFolder + "/" + Filename);
if (!System.IO.File.Exists(this.tempFilename) || Overwrite)
System.IO.File.Create(this.tempFilename).Close();
else
CustomSoapException("File Already Exists", Filename);
}
public Instance Initialize()
{
System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
this.instanceId = Guid.NewGuid().ToString();
Session.Add(this.instanceId, this);
return this;
}
public void AppendChunk(byte[] buffer, long offset, int bufferSize)
{
// make sure that the file exists
if (!System.IO.File.Exists(this.tempFilename))
CustomSoapException("Cannot Find Temp File", this.tempFilename);
long tempFilesize = new System.IO.FileInfo(this.tempFilename).Length;
// if the temp file size is not the same as the offset then something went wrong....
if (tempFilesize != offset)
CustomSoapException("Transfer Corrupted", String.Format("The file size is {0}, expected {1} bytes", tempFilesize, offset));
else
{
// offset matches the filesize, so the chunk is to be inserted at the end of the file.
System.IO.FileStream FileStream = new System.IO.FileStream(this.tempFilename, System.IO.FileMode.Append);
FileStream.Write(buffer, 0, bufferSize);
FileStream.Close();
}
}
public void RemoveFromSession()
{
if (Instance.GetInstanceById(this.instanceId) != null)
System.Web.HttpContext.Current.Session.Remove(this.instanceId);
}
public static Instance GetInstanceById(string InstanceId)
{
object o = System.Web.HttpContext.Current.Session[InstanceId];
if (o != null)
return o as Instance;
else
return null;
}
/// <summary>
/// Throws a soap exception. It is formatted in a way that is more readable to the client, after being put through the xml serialisation process
/// Typed exceptions don't work well across web services, so these exceptions are sent in such a way that the client
/// can determine the 'name' or type of the exception thrown, and any message that went with it, appended after a : character.
/// </summary>
/// <param name="exceptionName"></param>
/// <param name="message"></param>
public static void CustomSoapException(string exceptionName, string message)
{
throw new System.Web.Services.Protocols.SoapException(exceptionName + ": " + message, new System.Xml.XmlQualifiedName("BufferedUpload"));
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -