环境:Win7 + VS2008代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>Upload</title>
<script type="text/javascript" language="javaScript">
function addFile()
{
var str = '<INPUT type="file" size="100" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div style="text-align:center" >
<h3>多文件上传</h3>
<p id="MyFile"><input type="file" size="100" name="File"/></p>
<p>
<input type="button" value="增加(Add)" onclick="addFile()"/>
<input onclick="this.form.reset()" type="button" value="重置(ReSet)"/>
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</p>
<p>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</p>
</div>
</form>
</body>
</html>
C# 代码如下过长,放在回复中
向没有回家过年的兄弟姐妹们问声过年好,也预祝自己新春大吉。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>Upload</title>
<script type="text/javascript" language="javaScript">
function addFile()
{
var str = '<INPUT type="file" size="100" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div style="text-align:center" >
<h3>多文件上传</h3>
<p id="MyFile"><input type="file" size="100" name="File"/></p>
<p>
<input type="button" value="增加(Add)" onclick="addFile()"/>
<input onclick="this.form.reset()" type="button" value="重置(ReSet)"/>
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</p>
<p>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</p>
</div>
</form>
</body>
</html>
C# 代码如下过长,放在回复中
向没有回家过年的兄弟姐妹们问声过年好,也预祝自己新春大吉。
解决方案 »
- 我用的PDA带有红外,但不知道能不能用它当作串口来发送数据(485通讯)(寻找 欺世盗名来灌水 yangyzqo)
- 大家有没有遇到过richtextbox无法使用ctrl-v粘贴的怪事???
- 在程序中如何改變文件的屬,我要鎖定文件
- QQ刷Q币的好机会,有兴趣的朋友来参加吧,
- 数据绑定
- main方法中为什么可以包含非静态数据
- 在c#中,得到一个任意得时间比如:2004-11-25,怎么才能得到该日子所在得周一的日期?
- c# 如何获取猎豹浏览器当前页面的地址?
- RDLC 如何动态绑定列??
- 我如何能读取所有的odbc的名称
- C# 如何把Picturebox图片付给bitmap
- 问一个关于panel和datagridview滚动的问题!!!!
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
// datetime
using System.Globalization;public partial class _Default : System.Web.UI.Page
{
struct fileInfo
{
public string fileName;
public Boolean bTxt;
public Boolean bPdf;
public Boolean bZip; public void InitFileInfo()
{
fileName = "";
bTxt = false;
bPdf = false;
bZip = false;
}
}
private string[] FileType = { ".pdf", ".zip", ".txt" }; // legal extend file name
protected FileStream flog = null;
protected ArrayList zipArr = new ArrayList();
protected ArrayList pdfArr = new ArrayList();
protected ArrayList txtArr = new ArrayList();
protected ArrayList fileInfoArr = new ArrayList();
protected ArrayList upFileArr = new ArrayList(); private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack) this.SaveFiles();
}
private Boolean SaveFiles()
{
/*------ Traversing all elements of the file form.------*/
HttpFileCollection files = HttpContext.Current.Request.Files; /*------ Status information ------*/
StringBuilder strMsg = new System.Text.StringBuilder(); strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = fileName;// System.IO.Path.GetExtension(fileName);
if (!fileExtension.Contains("."))
{
Response.Write("该文件类型不允许上传!");
return false;
}
// check file extend name.
Boolean bl = CheckFileExtendName(ref fileName);
if (false == bl)
{
return bl;
}
}
}
CheckFileIsComplete();
writeLogFile(); for (int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile]; string fileName,fileExtension; fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
Boolean ret = false;
// get save path
if (0 != upFileArr.Count)
{
foreach (string strName in upFileArr)
{
foreach (string strExten in FileType)
{
if (fileExtension == (strName + strExten))
{
ret = true;
}
}
}
if (true == ret)
{
ret = false;
string strPath = "";
getSavePath(fileExtension, ref strPath);
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(strPath + "\\" + fileExtension);
strMsg.Append("上传文件的文件名:" + fileExtension + "<br>");
}
}
strStatus.Text = strMsg.ToString();
}
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
// Check file extend name and instead the legal file to up-file array list
private Boolean CheckFileExtendName(ref String strName)
{
int index = strName.LastIndexOf('.');
char[] c = strName.ToCharArray();
string strExtend = "";
bool bl = false;
string strOnlyName = ""; for (int i = 0; i < strName.Length - index; i++)
{
strExtend += c[index + i];
} foreach (string str in FileType)
{
if (str == strExtend)
{
bl = true;
for (int i = 0; i < index; i++)
{
strOnlyName += c[i];
} if (FileType[0] == strExtend)
{
pdfArr.Add(strOnlyName);
}
else if (FileType[1] == strExtend)
{
zipArr.Add(strOnlyName);
}
else
{
txtArr.Add(strOnlyName);
}
}
} if (false == bl)
{
Response.Write("该文件类型不允许上传!");
}
return bl;
}
// Check file is complete
// if you have a .zip, a .pdf and a .txt file with the same name, the file is complete.
private void CheckFileIsComplete()
{
int index = 0;
string tempName;
fileInfo fileinfo = new fileInfo();
int i = 0; for (index = 0; index < zipArr.Count; index++ )
{
fileinfo.InitFileInfo();
tempName = (string)zipArr[index]; fileinfo.fileName = tempName;
fileinfo.bZip = true; if ( 0 != pdfArr.Count)
{
for (i = 0; i < pdfArr.Count; i++)
{
if (tempName == (string)pdfArr[i])
{
fileinfo.bPdf = true;
pdfArr.RemoveAt(i);
break;
}
}
} if (0 != txtArr.Count)
{
for (i = 0; i < txtArr.Count; i++)
{
if (tempName == (string)txtArr[i])
{
fileinfo.bTxt = true;
txtArr.RemoveAt(i);
break;
}
}
}
fileInfoArr.Add(fileinfo);
} if (0 != pdfArr.Count)
{
for (index = 0; index < pdfArr.Count; index++)
{
fileinfo.InitFileInfo();
tempName = (string)pdfArr[index]; fileinfo.fileName = tempName;
fileinfo.bPdf = true; if (0 != txtArr.Count)
{
for (i = 0; i < txtArr.Count; i++)
{
if (tempName == (string)txtArr[i])
{
fileinfo.bTxt = true;
txtArr.RemoveAt(i);
break;
}
}
}
fileInfoArr.Add(fileinfo);
}
} if (0 != txtArr.Count)
{
for (i = 0; i < txtArr.Count; i++)
{
fileinfo.InitFileInfo(); tempName = (string)txtArr[index];
fileinfo.bTxt = true;
fileInfoArr.Add(fileinfo);
}
}
}
// write all file information to log file.
private void writeLogFile()
{
string strFileName;
string strLogInfo;
strFileName = "C:\\LogFile\\";
strFileName += DateTime.Now.ToString("yyyyMMddhhmmss");
strFileName += ".log"; Directory.CreateDirectory("LogFile");
flog = new FileStream(strFileName, FileMode.CreateNew);
foreach (fileInfo fInfo in fileInfoArr)
{
if (fInfo.bTxt && fInfo.bPdf && fInfo.bZip)
{
upFileArr.Add(fInfo.fileName);
}
else
{
strLogInfo = DateTime.Now.ToString("hh:mm:ss");
strLogInfo += " ";
strLogInfo += fInfo.fileName; if (!fInfo.bZip)
{
strLogInfo += " miss zip file.";
}
if (!fInfo.bPdf)
{
strLogInfo += " miss pdf file.";
}
if (!fInfo.bTxt)
{
strLogInfo += " miss txt file";
}
strLogInfo += "\n";
flog.Position = flog.Length;
Encoding encoder = Encoding.UTF8;
Byte[] bytes = encoder.GetBytes(strLogInfo);
flog.Write(bytes, 0, bytes.Length);
}
}
if( 0 != upFileArr.Count )
{
strLogInfo = DateTime.Now.ToString("hh:mm:ss");
strLogInfo += " ";
strLogInfo += "Complete files as following:\n";
foreach(string str in upFileArr)
{
strLogInfo += " ";
strLogInfo += str;
strLogInfo += "\n";
}
flog.Position = flog.Length;
Encoding encoder = Encoding.UTF8;
Byte[] bytes = encoder.GetBytes(strLogInfo);
flog.Write(bytes, 0, bytes.Length);
}
flog.Close();
} }还是放不下,剩下最好一个函数,只能放在下一个回复中了。
private void getSavePath(string fileName, ref string strPath)
{
string tempStr = fileName;
string langStr = "";
string vehicleStr = "";
string versionStr = "";
int index = 0;
int i = 0; langStr = tempStr.Substring(tempStr.LastIndexOf("_") + 1, tempStr.LastIndexOf('.') - tempStr.LastIndexOf('_')); while(-1 != index)
{
index = fileName.IndexOf('_');
i++;
if (3 == i)
{
vehicleStr = tempStr.Substring(0, index);
}
if (4 == i)
{
versionStr = tempStr.Substring(0, index);
}
tempStr = tempStr.Substring(index + 1);
}
strPath = "\\";
strPath += langStr;
Directory.CreateDirectory(strPath);
strPath = "\\";
strPath += vehicleStr;
Directory.CreateDirectory(strPath);
strPath = "\\";
strPath += versionStr;
Directory.CreateDirectory(strPath);
}
是点击上传之后,出现了页面不能显示的问题,我限定了只能上传.txt,.zip及.pdf的上传,而且必须是匹配了之后才能上传,之前报告了一个Logfile不能访问,我把"\LogFile"改成"C:\LogFile",然后就出现点击“上传”就不能显示页面了...
string path=Server.MapPath("LogFile");
string path=Server.MapPath("LogFile"); //"C:\\LogFile\\";
strFileName = DateTime.Now.ToString("yyyyMMddhhmmss");
strFileName += ".log"; Directory.CreateDirectory("LogFile");
flog = new FileStream(path+ "\\" + strFileName, FileMode.CreateNew);
我的文件只要小于2M,就可以显示页面,如果超过2M,就会出现不能显示页面,为什么呢?
web config中设置了HttpRuntime(代码如下):
<httpRuntime executionTimeout="90" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"
minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="false"/>
求指导!
是权限问题。
code=csharp] private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack) this.SaveFiles();
}[code]
打断点,程序没有进断点,直接报告显示不了。
minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="false"/>插入<system.web></system.web>即可。谢谢诸位!