ASP.NET大ファイルブレークアップ
26518 ワード
フロントエンドコード
バックエンドコード
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script language="javascript">
//
function CheckAll(e, itemname) {
var aa = document.getElementsByName(itemname);
if (aa == undefined) return;
for (var i = 0; i < aa.length; i++) aa[i].checked = e.checked;
}
</script>
<script language="javascript">
//
function delss() {
var checkArray = document.getElementsByName("Item");
var checkNum = 0;
for (var i = 0; i < checkArray.length; i++) {
if (checkArray[i].checked) { checkNum++; }
}
if (checkNum == 0) {
alert(' , !');
return false;
}
else {
if (confirm(" ?")) {
return true;
} else {
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="tick" />
</Triggers>
</asp:UpdatePanel>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text=" " OnClick="Button1_Click" />
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr class="spgl_bg">
<td width="6%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
<input name="chkall" value="1" type="checkbox" onclick="CheckAll(this,'Item')" class="qx" />
</td>
<td width="6%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
</td>
<td width="16%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
</td>
<td width="52%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
</td>
<td width="8%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
</td>
<td width="12%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">
</td>
</tr>
<asp:Repeater ID="rptList" runat="server">
<ItemTemplate>
<tr>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>">
<input name="Item" type="checkbox" value='<%# Eval("fid")%>' />
</td>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>">
<%# Container.ItemIndex + 1 %>
</td>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>"
class="gml">
<%# Eval("FileNameLocal")%>
</td>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>"
class="gml">
<%# Eval("FilePathLocal")%>
</td>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>"
class="gml">
<%# Eval("PostedPercent")%>
</td>
<td align="center" valign="middle" bgcolor="<%#(Container.ItemIndex%2==0)?"#FFFFFF":"#f2f2f2"%>">
<asp:LinkButton ID="lbTop" runat="server" Text=' ' CommandArgument='<%# Eval("fid") %>'
OnCommand="lbTop_Click"></asp:LinkButton>|
<asp:LinkButton ID="lbClose" Visible="false" runat="server" Text=' ' CommandArgument='<%# Eval("fid") %>'
OnCommand="lbClose_Click"></asp:LinkButton>
<asp:LinkButton ID="lbDel" runat="server" Text=" " OnCommand="lbDel_Click" CommandArgument='<%# Eval("fid") %>'
OnClientClick="javascript:return confirm(' ?');"></asp:LinkButton>
</td>
<asp:HiddenField ID="hidOK" runat="server" Value='<%# Eval("Isok") %>' />
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="btnAllDelete" runat="server" Text=" " class="plsc" CommandName="delete"
OnClientClick="return delss()" OnCommand="btnAllDelete_Command" />
</form>
</body>
</html>
バックエンドコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.demoSql.db;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Threading;
namespace Web
{
public partial class Index : System.Web.UI.Page
{
#region
public long m_FileSize; // 。
long m_RangePos; //
public long RangePos
{
set { this.m_RangePos = value; }
}
// , ,
static ReaderWriterLock m_writeLock = new ReaderWriterLock();
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetList();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
#region
string md5Fisrt = GenerateCheckCode10();
string uidFisrt = "0";
string fileLengthFisrt = FileUpload1.PostedFile.ContentLength.ToString();
string fileSizeFisrt = Math.Round(Convert.ToDouble(FileUpload1.PostedFile.ContentLength) / Convert.ToDouble(1024 * 1024), 2).ToString() + "MB";
string pathLocalFisrt = FileUpload1.PostedFile.FileName;
#endregion
string FileType = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
if (FileUpload1.FileName == "" || Request.Files.Count < 0)
{
string msg = " !";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');</script>");
}
else if (FileType.ToLower() != "rar")
{
string msg = " !";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');</script>");
}
else
{
#region
HttpUploaderDB db = new HttpUploaderDB();
xdb_files inf = new xdb_files();
inf.FileNameLocal = Path.GetFileName(pathLocalFisrt);
inf.FilePathLocal = pathLocalFisrt;
inf.FileNameRemote = md5Fisrt + Path.GetExtension(pathLocalFisrt).ToLower();
//
if (db.GetFileInfByMd5(md5Fisrt, ref inf))
{
inf.FileNameLocal = Path.GetFileName(pathLocalFisrt);
inf.FilePathLocal = pathLocalFisrt;
inf.uid = int.Parse(uidFisrt);// UID UID
inf.IsDeleted = false;
inf.fid = db.Add(ref inf);
}//
else
{
HttpUploaderCfg cfg = new HttpUploaderCfg();
cfg.CreateUploadPath();
inf.uid = int.Parse(uidFisrt);// UID UID
inf.FileSize = fileSizeFisrt;
inf.FileMD5 = md5Fisrt;
inf.FileLength = Convert.ToInt32(fileLengthFisrt);
inf.FilePathRemote = Path.Combine(cfg.GetUploadPath(), inf.FileNameRemote);
inf.FilePathRelative = Path.Combine(cfg.GetRelatPath(), inf.FileNameRemote);
inf.fid = db.Add(ref inf);
GetList();
}
#endregion
#region
int num = 0;
string struid = uidFisrt;
string strfid = inf.fid.ToString();
string strFileSize = fileLengthFisrt;
string strRangePos = num.ToString();
if (strFileSize.Length > 0
&& struid.Length > 0
&& strfid.Length > 0
&& md5Fisrt.Length > 0
&& strRangePos.Length > 0
&& Request.Files.Count > 0)
{
long fileSize = Convert.ToInt64(strFileSize);
long rangePos = Convert.ToInt64(strRangePos);
int uid = Convert.ToInt32(struid);
int fid = Convert.ToInt32(strfid);
//
HttpPostedFile file = Request.Files.Get(0);
long rangeSize = 131072;
long rangeSiHH = 131072;
xdb_files infModel = new xdb_files();
for (long i = 0; i <= Convert.ToInt32(file.InputStream.Length); )
{
if (db.GetFileInfByFid(fid, ref infModel))
{
// = +
//long postedLength = rangePos + rangeSize;
long postedLength = i + rangeSize;
long nownum = i;
if (file.InputStream.Length < postedLength)
{
postedLength = file.InputStream.Length - nownum;
rangeSiHH = file.InputStream.Length - nownum;
}
// = /
double per = ((double)postedLength / (double)fileSize) * 100;
decimal perd = Math.Round((decimal)per, 2);
string postedPercent = perd.ToString() + "%";
//
FileResumer res = new FileResumer();
res.m_FileSize = fileSize;//
res.RangePos = rangePos;
#region
m_writeLock.AcquireWriterLock(1000);
//
if (!File.Exists(infModel.FilePathRemote))
{
FileStream fs = File.OpenWrite(infModel.FilePathRemote);
BinaryWriter w = new BinaryWriter(fs);
for (long ii = 0; ii < infModel.FileLength; ++ii)
{
w.Write((byte)0);
}
w.Close();
fs.Close();
}
m_writeLock.ReleaseWriterLock();
//
if (rangeSiHH > 0)
{
// ,
// 。
FileStream fs = new FileStream(infModel.FilePathRemote, FileMode.Open, FileAccess.Write, FileShare.Write);
fs.Seek(i, SeekOrigin.Begin);
byte[] ByteArray = new byte[rangeSiHH];
file.InputStream.Read(ByteArray, 0, (int)rangeSiHH);
fs.Write(ByteArray, 0, (int)rangeSiHH);
fs.Flush();
fs.Close();
}
#endregion
//
db.UpdateProgress(uid, fid, rangePos, postedLength, postedPercent);
i += rangeSize;
}
}
}
#endregion
#region
db.UploadComplete(md5Fisrt);
#endregion
GetList();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbTop_Click(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
HttpUploaderDB db = new HttpUploaderDB();
#region
foreach (RepeaterItem grv in rptList.Items)
{
LinkButton lb1 = (LinkButton)rptList.Items[grv.ItemIndex].FindControl("lbTop");//
LinkButton lb2 = (LinkButton)rptList.Items[grv.ItemIndex].FindControl("lbClose");//
lb1.Enabled = false;
lb2.Enabled = false;
}
LinkButton t = (LinkButton)sender;
RepeaterItem ri = (RepeaterItem)t.NamingContainer;
LinkButton itemid = ((LinkButton)ri.FindControl("lbClose"));
itemid.Enabled = true;
#endregion
#region
string md5Fisrt = GenerateCheckCode10();
string uidFisrt = "0";
string fileLengthFisrt = FileUpload1.PostedFile.ContentLength.ToString();
string fileSizeFisrt = Math.Round(Convert.ToDouble(FileUpload1.PostedFile.ContentLength) / Convert.ToDouble(1024 * 1024), 2).ToString() + "MB";
string pathLocalFisrt = FileUpload1.PostedFile.FileName;
xdb_files model = db.GetModelByID(id);
md5Fisrt = model.FileMD5;
string FileType = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
if (FileUpload1.FileName == "" || Request.Files.Count < 0)
{
string msg = " !";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');</script>");
}
else if (pathLocalFisrt != model.FilePathLocal || fileLengthFisrt != Convert.ToString(model.FileLength))
{
string msg = " !";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');</script>");
}
else if (FileType.ToLower() != "rar")
{
string msg = " !";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');</script>");
}
else
{
#region
int num = 0;
string struid = uidFisrt;
string strfid = id.ToString();
string strFileSize = fileLengthFisrt;
string strRangePos = num.ToString();
if (strFileSize.Length > 0
&& struid.Length > 0
&& strfid.Length > 0
&& md5Fisrt.Length > 0
&& strRangePos.Length > 0
&& Request.Files.Count > 0)
{
long fileSize = Convert.ToInt64(strFileSize);
long rangePos = Convert.ToInt64(strRangePos);
int uid = Convert.ToInt32(struid);
int fid = Convert.ToInt32(strfid);
//
HttpPostedFile file = Request.Files.Get(0);
long rangeSize = 131072;
long rangeSiHH = 131072;
xdb_files infModel = new xdb_files();
for (long i = model.FilePos; i <= Convert.ToInt32(file.InputStream.Length); )
{
if (db.GetFileInfByFid(fid, ref infModel))
{
// = +
//long postedLength = rangePos + rangeSize;
long postedLength = i + rangeSize;
long nownum = i;
if (file.InputStream.Length < postedLength)
{
postedLength = file.InputStream.Length - nownum;
rangeSiHH = file.InputStream.Length - nownum;
}
// = /
double per = ((double)postedLength / (double)fileSize) * 100;
decimal perd = Math.Round((decimal)per, 2);
string postedPercent = perd.ToString() + "%";
//
FileResumer res = new FileResumer();
res.m_FileSize = fileSize;//
res.RangePos = rangePos;
#region
m_writeLock.AcquireWriterLock(1000);
//
if (!File.Exists(infModel.FilePathRemote))
{
FileStream fs = File.OpenWrite(infModel.FilePathRemote);
BinaryWriter w = new BinaryWriter(fs);
for (long ii = 0; ii < infModel.FileLength; ++ii)
{
w.Write((byte)0);
}
w.Close();
fs.Close();
}
m_writeLock.ReleaseWriterLock();
//
if (rangeSiHH > 0)
{
// ,
// 。
FileStream fs = new FileStream(infModel.FilePathRemote, FileMode.Open, FileAccess.Write, FileShare.Write);
fs.Seek(i, SeekOrigin.Begin);
byte[] ByteArray = new byte[rangeSiHH];
file.InputStream.Read(ByteArray, 0, (int)rangeSiHH);
fs.Write(ByteArray, 0, (int)rangeSiHH);
fs.Flush();
fs.Close();
}
#endregion
//
db.UpdateProgress(uid, fid, rangePos, postedLength, postedPercent);
i += rangeSize;
}
}
}
#endregion
#region
db.UploadComplete(md5Fisrt);
#endregion
GetList();
}
#endregion
EnabledStop();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbClose_Click(object sender, CommandEventArgs e)
{
EnabledStop();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbDel_Click(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
HttpUploaderDB db = new HttpUploaderDB();
db.Del(id);
GetList();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAllDelete_Command(object sender, CommandEventArgs e)
{
HttpUploaderDB db = new HttpUploaderDB();
string ids = Request.Form["Item"];
switch (e.CommandName)
{
case "delete":
BakDelete(ids);
break;
}
GetList();
}
/// <summary>
///
/// </summary>
/// <param name="ids"></param>
private void BakDelete(string ids)
{
HttpUploaderDB db = new HttpUploaderDB();
string strapp = ids;
string[] strtemp = strapp.Split(',');
foreach (string strs in strtemp)
{
db.Del(Convert.ToInt32(strs.ToString()));
}
}
/// <summary>
///
/// </summary>
public void GetList()
{
HttpUploaderDB db = new HttpUploaderDB();
rptList.DataSource = db.GetList();
rptList.DataBind();
EnabledStop();
}
/// <summary>
/// ,
/// </summary>
public void EnabledStop()
{
foreach (RepeaterItem grv in rptList.Items)
{
LinkButton lb1 = (LinkButton)rptList.Items[grv.ItemIndex].FindControl("lbTop");//
LinkButton lb2 = (LinkButton)rptList.Items[grv.ItemIndex].FindControl("lbClose");//
HiddenField hidk = (HiddenField)rptList.Items[grv.ItemIndex].FindControl("hidOK");//
if (hidk.Value.ToLower() == "true")
{
lb1.Enabled = false;
lb2.Enabled = false;
}
else
{
lb1.Enabled = true;
lb2.Enabled = false;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
//GetList();
}
private string GenerateCheckCode10()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 10; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
return checkCode;
}
}
}