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

asp.net 上传文件夹

   如何使用asp.net 写一个程序,把本地的文件夹上传到ftp 服务器上面,前提是ftp服务器不运行此asp.net的程序,请各位大侠多多指教!! --------------------编程问答-------------------- http://www.cnblogs.com/Jwin/archive/2008/10/28/1320970.html --------------------编程问答-------------------- 普通的上传文件就可以实现

服务器不运行? 

using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 上传文件到指定的文件夹
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSure_Click(object sender, EventArgs e)
    {
        //string phName = this.txtName.Text;
        //string phType = this.ddlType.SelectedValue;

        if (this.myFile.PostedFile != null)  //myFile是上传控件的名称
        {
            string photoName1 = myFile.PostedFile.FileName; //获取初始文件名
            int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
            string newext = photoName1.Substring(i); //获取文件扩展名
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
            {
                Response.Write("文件格式不正确!");
                Response.End();
            }
            DateTime now = DateTime.Now; //获取系统时间

            string classid = DateTime.Now.Year.ToString();

            //根据年份判断在该路径下是否存在以当年年份文件夹 否则将建立以该年份的文件夹
            if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))                //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。 File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
            {
                System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);              //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
            }
            string photoName2 = now.Millisecond.ToString() + "_" + myFile.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
            myFile.PostedFile.SaveAs(Server.MapPath("photos\\"+classid+"\\" + photoName2)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
        }

    }
    /// <summary>
    /// 这是一个button控件指定在某目录下创建文件夹的示例,可供参考按照年月或不同的人上传在不同的文件夹里
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCreateBag_Click(object sender, EventArgs e)
    {
        string classid = DateTime.Now.Year.ToString();
        if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))                //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。 File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
        {
            System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);              //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。

        }
        else
        {
            Response.Write("<script language=javascript>alert('文件夹已经存在')</script>");
        }

    }
}


然后设置你服务器文件夹权限 --------------------编程问答--------------------
服务器不需要运行啊,只要你有ftp用户名和密码,

用文件用流的方式传上去就行了。


FTP ftp = new FTP("192.168.1.117", "ahuinan", "123456"); 

string oldname = this.FileUpload1.PostedFile.FileName; 
string newname = dd + h + mm + ss + oldname.Substring(oldname.LastIndexOf(".")); 


// ftp.Connect(); 
ftp.OpenUpload(oldname,newname); 
while (ftp.DoUpload() > 0) 

int perc = (int)(((ftp.BytesTotal) * 100) / ftp.FileSize); 
Response.Write(perc.ToString() + "%<br/>"); 
Response.Flush(); 
--------------------编程问答-------------------- ftpwebrequest
private static void UploadFile(string localFile)  
{  
FileInfo fi = new FileInfo(localFile);  
FileStream fs = fi.OpenRead();  
long length = fs.Length;  
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);  
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);  
req.Method = WebRequestMethods.Ftp.UploadFile;  
req.UseBinary = true;  
req.ContentLength = length;  
req.Timeout = 10 * 1000;  
try  
{  
Stream stream = req.GetRequestStream();  
int BufferLength = 2048;  
byte[] b = new byte[BufferLength];  
int i;  
while ((i = fs.Read(b, 0, BufferLength)) > 0)  
{  
stream.Write(b, 0, i);  
}  
stream.Close();  
stream.Dispose();  

}  
catch (Exception ex)  
{  
}  

}  
--------------------编程问答-------------------- http://www.25yi.com/wangyesheji/netftp-162/ --------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,