自分で書いたファイルにアップロードされたクラス
6568 ワード
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.Remoting.Messaging;
namespace uploadfiles
{
/// <summary>
/// ClassUpload 。
/// </summary>
/*
wenconfig
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
<!-- maxRequestLength: ASP.NET
HTTP 。 。 KB 。 4096 KB
(4 MB)。executionTimeout: ASP.NET , 。 ,
DNS , , 。
-->
*/
/*
Machine.config
<processModel
enable="true"
timeout="15"
idleTimeout="25"
shutdownTimeout="5"
requestLimit="1000"
requestQueueLimit="500"
responseDeadlockInterval="00:03:00"
responseRestartDeadlockInterval="Infinite"
memoryLimit="80"
webGarden="true"
maxWorkerThreads="25"
maxIoThreads="25"/>
<!--
, :</P><XMP> aspnet_wp.exe (PID: 1520) , 460 MB( RAM 60)。
</XMP>
<P> , Machine.config <processModel> memoryLimit 。
-->
*/
public class ClassUpload
{
public ClassUpload()
{
//
// TODO:
//
}
private System.IO.MemoryStream m;
private System.IO.Stream fs;
private string _filename; //
private int _maxlength; // ( )
private string _FilePath; //
private int _length = 0; // ( )
private string _savepath; //
private string _savename; //
private string _fullpath; //
/// <summary>
/// ( )
/// </summary>
public int length
{
get
{
return _length;
}
}
/// <summary>
///
/// </summary>
public string savepath
{
get
{
return _savepath;
}
}
/// <summary>
///
/// </summary>
public string savename
{
get
{
return _savename;
}
}
/// <summary>
///
/// </summary>
public string fullpath
{
get
{
return _fullpath;
}
}
/// <summary>
///
/// </summary>
public string filepath
{
get
{
return _FilePath;
}
}
/// <summary>
///
/// </summary>
/// <param name="MyFile">System.Web.UI.HtmlControls.HtmlInputFile</param>
/// <param name="savepath"> </param>
/// <param name="maxlength"> </param>
public string upload(System.Web.UI.HtmlControls.HtmlInputFile MyFile,string savepath,int maxlength)
{
this._savepath = savepath;
this._maxlength = maxlength;
//
if(MyFile.PostedFile != null)
{
System.Web.HttpFileCollection oFiles;
oFiles = System.Web.HttpContext.Current.Request.Files;
if(oFiles.Count < 1)
{
return " !";
}
_FilePath = oFiles[0].FileName;
if(_FilePath == "" || _FilePath == null)
{
return " !";
}
_filename = "."+_FilePath.Substring(_FilePath.LastIndexOf(".")+1);
//_contentlength = oFiles[0].ContentLength.ToString();
//Response.Write(FileName+FilePath);
if(oFiles[0].ContentLength>this._maxlength)
{
return " ";
}
else
{
try
{
// 。
byte[] b = new byte[oFiles[0].ContentLength];
//System.IO.Stream fs;
fs = (System.IO.Stream)oFiles[0].InputStream;
fs.Read(b, 0, oFiles[0].ContentLength);
// UploadFile
return this.UploadFile(b,this._filename,this._savepath);
//int value = Convert.ToInt32(asy.AsyncState);
}
catch(Exception ex)
{
//Response.Write(ex.Message);
return ex.Message;
}
finally
{
fs.Close();
}
}
}
else
{
//Response.Write(" ");
return " ";
}
}
private string UploadFile(byte[] fs,string FileName,string savepath)
{
try
{
// , 。
m = new MemoryStream(fs);
this._length = fs.Length;
//System.Web.HttpContext.Current.Server.MapPath(".") + @"\\images\"
// , 。
this._fullpath = this._savepath+ names()+FileName;
FileStream f = new FileStream(this._fullpath, FileMode.Create);
this._savename = names()+FileName;
//
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return " !";
}
catch(Exception ex)
{
return ex.Message;
}
}
// ( , , , , , )+
private string names()
{
Random rm = new Random(System.Environment.TickCount);
return System.DateTime.Now.ToString("yyyyMMddhhmmss")+rm.Next(1000,9999).ToString();
}
}
}