首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net文件夹上传怎么解决

2012-05-28 
asp.net文件夹上传如何解决?我在做的时候,可能需要上传文件夹,如何解决?[解决办法]把文件夾壓縮后再上傳,

asp.net文件夹上传如何解决?
我在做的时候,可能需要上传文件夹,如何解决?

[解决办法]
把文件夾壓縮后再上傳,上傳后再解壓,下面是代碼,可能需要相應做修改
1.拷贝Rar.exe到程序根目录下 
2.将要上传的文件打包成rar文件,注意我目前做的是该rar下直接包含图片文件,如1.rar下包含a.jpg,b.jpg等 
3.aspx页面代码 
<table border=0 cellPadding=4 cellSpacing=1 align="center" class=table-Kang> 
<TR>
<TD colSpan=4 align="center"> <div class=biaoti>图片批量上传 </div> </TD>
</TR>

<tr> 
<td align=right class=table-TitleBG>上传路径: </td> 
<td align=left class=table-Sbg> <asp:FileUpload ID="myfile" runat="server" Width="400px" /> </td> 
</tr> 
<tr> 
<td colspan=2 align=center class=table-TitleBG> <asp:Button ID="Button1" runat="server" Text="上 传" OnClick="Button1_Click" /> 
td> 
</tr> 
</table> 
4.cs代码(注意:下面代码有设计到数据库方面的,要相应做修改) 
protected void Button1_Click(object sender, EventArgs e) 

if (myfile.HasFile) 

if (myfile.PostedFile.FileName.EndsWith(".rar") ¦ ¦ myfile.PostedFile.FileName.EndsWith(".RAR")) 
#region//rar文件 

string strListCode = Request.QueryString["sListCode"]; 
string strCategory = Request.QueryString["Category"]; 
//当前虚拟目录路径 
string _strPicSavePath = "../"; 
string strGenerPath = GetPictureid(); 
string strDir = HttpContext.Current.Server.MapPath(_strPicSavePath + "Picture/" + strGenerPath + "/");//图片的保存目录 
if (!Directory.Exists(strDir)) 

Directory.CreateDirectory(strDir); 

string path = strDir + myfile.PostedFile.FileName.Substring(myfile.PostedFile.FileName.LastIndexOf("\\")); 
myfile.PostedFile.SaveAs(path); 
//Response.Write("文件上传成功:" + path); 
//Response.End(); 
// 在此处放置用户代码以初始化页面
Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardInput = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.FileName = "cmd.exe"; 
p.Close(); 
//解压Rar文件
// Response.Write(HttpContext.Current.Server.MapPath(_strPicSavePath)); 
string ServerDir = HttpContext.Current.Server.MapPath(_strPicSavePath); //@"C:\Program Files\WinRAR";//rar路径
System.Diagnostics.Process Process1 = new Process(); 
Process1.StartInfo.FileName = ServerDir + "\\Rar.exe"; 
Directory.CreateDirectory(path + ".files"); //创建解压文件夹
Process1.StartInfo.Arguments = " x -inul -y " + path + " " + path + ".files"; 
Process1.Start();//解压开始
while (!Process1.HasExited) //等待解压的完成




File.Delete(path);//删除rar文件
string strFileName = myfile.PostedFile.FileName.Substring(myfile.PostedFile.FileName.LastIndexOf("\\")); 
string strFileNames = strFileName.Substring(1, strFileName.Length - 1); 
strFileName = strFileNames.Substring(0, strFileNames.Length - 4); 
//解压后文件夹绝对路径 
string strCurPath = path + ".files" + "\\" + strFileName + "\\"; 
//解压后文件相对路径 
string strSavePath = _strPicSavePath + "Picture/" + strGenerPath + "/" + strFileNames + ".files/" + strFileName + "/"; 
foreach (string flName in Directory.GetFiles(strCurPath)) 

string strName = flName.Substring(flName.LastIndexOf("\\") + 1);//文件名 
string strDataPath = strSavePath + strName;//数据库保存路径 
string strProdNames = strName.Split('.')[0];//款号+序号 
string strProdName;//款号 
string strTitle;//标题 
if (strProdNames.Split('-').Length > 1) 

strProdName = strProdNames.Split('-')[0]; 
strTitle = strProdNames; 

else 

strProdName = strProdNames; 
strTitle = strProdNames; 


string strSql = "insert into xpvProdPic(sProdCode,spath,sremark,spictureName,sPicTitle) values ('" + strProdName + "','" + strDataPath + "','','" + strName + "','"+strTitle+"' )"; 
DbHelperSQL.ExecuteSql(strSql); 


string messageContent = "上传成功!"; 
StringHelp.WinAlert(Page, messageContent); 
//Page.RegisterStartupScript("d", " <script language='javascript'>ReturnBefore('" + strListCode + "','" + strCategory + "'); </script>"); 


#endregion 
else//不能上传 

string messageContent = "请选择rar文件!"; 
StringHelp.WinAlert(Page, messageContent); 


else 

string messageContent = "请选择要上传的文件!"; 
StringHelp.WinAlert(Page, messageContent); 



 private string GetPictureid() 

string id = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0') + System.DateTime.Now.Hour.ToString().PadLeft(2, '0') + System.DateTime.Now.Minute.ToString().PadLeft(2, '0') + System.DateTime.Now.Millisecond.ToString().PadLeft(3, '0'); 
//string id=id+ 
int i = new System.Random().Next(0, 5); 
int k = new System.Random().Next(0, 9); 
id = id + i.ToString() + k.ToString(); 
return id; 

 
 
 

[解决办法]
压缩在上传吧

然后服务器上自动解压。


protected void Button1_Click(object sender, EventArgs e)
{
if (myfile.HasFile)
{
if (myfile.PostedFile.FileName.EndsWith(".rar") ¦ ¦ myfile.PostedFile.FileName.EndsWith(".RAR"))


#region//rar文件
{
string strListCode = Request.QueryString["sListCode"];
string strCategory = Request.QueryString["Category"];
//当前虚拟目录路径
string _strPicSavePath = "../";
string strGenerPath = GetPictureid();
string strDir = HttpContext.Current.Server.MapPath(_strPicSavePath + "Picture/" + strGenerPath + "/");//图片的保存目录
if (!Directory.Exists(strDir))
{
Directory.CreateDirectory(strDir);
}
string path = strDir + myfile.PostedFile.FileName.Substring(myfile.PostedFile.FileName.LastIndexOf("\\"));
myfile.PostedFile.SaveAs(path);
//Response.Write("文件上传成功:" + path);
//Response.End();
// 在此处放置用户代码以初始化页面
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "cmd.exe";
p.Close();
//解压Rar文件
// Response.Write(HttpContext.Current.Server.MapPath(_strPicSavePath));
string ServerDir = HttpContext.Current.Server.MapPath(_strPicSavePath); //@"C:\Program Files\WinRAR";//rar路径
System.Diagnostics.Process Process1 = new Process();
Process1.StartInfo.FileName = ServerDir + "\\Rar.exe";
Directory.CreateDirectory(path + ".files"); //创建解压文件夹
Process1.StartInfo.Arguments = " x -inul -y " + path + " " + path + ".files";
Process1.Start();//解压开始
while (!Process1.HasExited) //等待解压的完成
{
}
File.Delete(path);//删除rar文件
string strFileName = myfile.PostedFile.FileName.Substring(myfile.PostedFile.FileName.LastIndexOf("\\"));
string strFileNames = strFileName.Substring(1, strFileName.Length - 1);
strFileName = strFileNames.Substring(0, strFileNames.Length - 4);
//解压后文件夹绝对路径
string strCurPath = path + ".files" + "\\" + strFileName + "\\";
//解压后文件相对路径
string strSavePath = _strPicSavePath + "Picture/" + strGenerPath + "/" + strFileNames + ".files/" + strFileName + "/";
foreach (string flName in Directory.GetFiles(strCurPath))
{
string strName = flName.Substring(flName.LastIndexOf("\\") + 1);//文件名
string strDataPath = strSavePath + strName;//数据库保存路径
string strProdNames = strName.Split('.')[0];//款号+序号
string strProdName;//款号
string strTitle;//标题
if (strProdNames.Split('-').Length > 1)
{
strProdName = strProdNames.Split('-')[0];
strTitle = strProdNames;
}
else
{
strProdName = strProdNames;
strTitle = strProdNames;
}

string strSql = "insert into xpvProdPic(sProdCode,spath,sremark,spictureName,sPicTitle) values ('" + strProdName + "','" + strDataPath + "','','" + strName + "','"+strTitle+"' )";


DbHelperSQL.ExecuteSql(strSql);

}
string messageContent = "上传成功!";
StringHelp.WinAlert(Page, messageContent);
//Page.RegisterStartupScript("d", " <script language='javascript'>ReturnBefore('" + strListCode + "','" + strCategory + "'); </script>");

}
#endregion
else//不能上传
{
string messageContent = "请选择rar文件!";
StringHelp.WinAlert(Page, messageContent);
}
}
else
{
string messageContent = "请选择要上传的文件!";
StringHelp.WinAlert(Page, messageContent);
}
}

 private string GetPictureid()
{
string id = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0') + System.DateTime.Now.Hour.ToString().PadLeft(2, '0') + System.DateTime.Now.Minute.ToString().PadLeft(2, '0') + System.DateTime.Now.Millisecond.ToString().PadLeft(3, '0');
//string id=id+
int i = new System.Random().Next(0, 5);
int k = new System.Random().Next(0, 9);
id = id + i.ToString() + k.ToString();
return id;
}


就像这样的代码。

热点排行