当前位置:编程学习 > C#/ASP.NET >>

C# 文件夹上传 给个实例

项目需求:文件夹中全是图片格式,内涵子文件夹!需要将其上传到服务器!
提供思路是这样:遍历整个文件夹,将文件夹中的图片循环上传到服务器!并按照图片名称重新归类到新的文件夹中!困难,没有什么控件可以选取整个文件夹!

求助大神,有没有什么好的方法进行上传!网上找了很多案例,都不能用!
比如用webclient类来上传!!没又找到相关的完整的列子!
~希望大神能给我提供个方法!最好有个简单的列子!~感谢~ C# .NET 服务器 上传 文件夹 --------------------编程问答-------------------- 文件夹上传?闻所未闻。等楼下解决。 --------------------编程问答-------------------- 文件夹上传是不可能的,但你可以把文件的路径、文件夹名称上传给服务器、让服务器创建文件夹,并把属于该文件夹里面的文件放在里面 --------------------编程问答-------------------- 可以采用上传到ftp服务器方式
--------------------编程问答-------------------- 可以用SVN中的commit命令上传到你的服务器上! --------------------编程问答-------------------- 居然有人说不可能!!
用一个压缩类,把文件夹压缩ZIP,然后把这个ZIP文件上传。
接收处,接收到文件,解压到指定路径。
OK,完成!
PS:如果文件大的话,当然还要解决分段续传! --------------------编程问答-------------------- 可以用ftp来上传文件,递归遍历文件夹找到所有文件。给你一个ftp操作类给你:http://blog.csdn.net/guwei4037/article/details/9501773 --------------------编程问答-------------------- 文件夹中的图片循环上传到服务器!并按照图片名称重新归类到新的文件夹中!困难?

这都困难,那还写什么程序?

没有什么控件可以选取整个文件夹!

使用FolderBrowserDialog控件。 --------------------编程问答-------------------- 应该是上传文件吧,怎么可能上传文件夹了,具体上传文件例子:http://jeffreyzhao.cnblogs.com/archive/2008/02/12/aspnet_upload.html --------------------编程问答-------------------- 上传文件时上传RAR压缩文件,上传后自动解压缩并删除压缩文件,下载文件时自动压缩下载的是RAR压缩文件。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//
using System.IO;
using System.Net;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

public partial class Default3 : System.Web.UI.Page
{
    ZipOutputStream zos = null;
    String strBaseDir = "";
    public string cutStr = "";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (myfile.HasFile)
        {
            if (myfile.PostedFile.FileName.EndsWith(".rar") || myfile.PostedFile.FileName.EndsWith(".RAR"))
            {
                string strGenerPath = GetPictureid();
                string filename = Path.GetFileNameWithoutExtension(myfile.PostedFile.FileName);
                string strDir = HttpContext.Current.Server.MapPath("~/Picture/" + strGenerPath + "/");
                string unrar = Server.MapPath("~/Picture/" + strGenerPath + "/");//加压文件夹
                if (!Directory.Exists(strDir))
                {
                    Directory.CreateDirectory(strDir);
                }
                string path = strDir + myfile.PostedFile.FileName.Substring(myfile.PostedFile.FileName.LastIndexOf("\\"));
                myfile.PostedFile.SaveAs(path);
                // 在此处放置用户代码以初始化页面   
                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文件   
                string ServerDir = @"C:\Program Files\WinRAR";

                System.Diagnostics.Process Process1 = new Process();
                Process1.StartInfo.FileName = ServerDir + "\\Rar.exe";
                Process1.StartInfo.Arguments = " x -inul -y " + path + " " + unrar + "";
                Process1.Start();//解压开始   
                while (!Process1.HasExited) //等待解压的完成   
                {
                }
                File.Delete(path);//删除rar文件   
            }
            else
            {
                Response.Write("<script>alert('请选择RAR文件');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请选择要上传的文件');</script>");
        }
    }
    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');
        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 btnDownload_Click(object sender, EventArgs e)
    {
        string filepath = Server.MapPath("~/Picture/20101228151901500/");
        dlZipDir(filepath, "导出文件");
    }
    void dlZipDir(string strPath, string strFileName)
    {
        MemoryStream ms = null;
        Response.ContentType = "application/octet-stream";
        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
        Response.AddHeader("Content-Disposition", "attachment;   filename=" + strFileName + ".zip");
        ms = new MemoryStream();
        zos = new ZipOutputStream(ms);
        strBaseDir = strPath;
        addZipEntry(strBaseDir);
        zos.Finish();
        zos.Close();
        Response.Clear();
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

    //文件下载后加压缩,每层文件夹连接是网站的绝对目录
    void addZipEntry(string PathStr)
    {
        DirectoryInfo di = new DirectoryInfo(PathStr);
        foreach (DirectoryInfo item in di.GetDirectories())
        {
            addZipEntry(item.FullName);
        }
        foreach (FileInfo item in di.GetFiles())
        {
            FileStream fs = File.OpenRead(item.FullName);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            string strEntryName = item.FullName.Replace(strBaseDir, "");
            ZipEntry entry = new ZipEntry(strEntryName);
            zos.PutNextEntry(entry);
            zos.Write(buffer, 0, buffer.Length);
            fs.Close();
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,