日期:2014-05-17  浏览次数:20460 次

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();//解压开始