国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

.netftp上傳文件方法

2019-11-14 14:20:28
字體:
供稿:網(wǎng)友

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Configuration;

namespace DlnOffice.general.netdisk
{
public partial class fileupload2 : System.Web.UI.Page
{

//這三個常量是從webconfig獲取
PRivate string ftpServerip = ConfigurationManager.AppSettings["ftpServerIP"];
private string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
private string ftpPassWord = ConfigurationManager.AppSettings["ftpPassword"];
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
up_FTP(FileUpload1);
//up_FTP(FileUpload2);
Response.Write("<script>alert('完成');window.location.href ='fileupload2.aspx'</script>");

//else
//{
// Response.Write("<script>alert('未選擇文件');</script>");
// return;
//}
}

public void up_FTP(FileUpload fu)
{
string AppPath = (base.Request.QueryString["parent_folder"] != null) ? base.Request.QueryString["parent_folder"].ToString() : "/信息科/常用軟件";
if (string.IsNullOrEmpty(AppPath))
{
base.Response.Redirect("index.aspx");
return;
}
//文件上傳地址根目錄,這里通過IIS架設(shè)本地主機為FTP服務(wù)器
//string FileSaveUri = @"ftp://127.0.0.1/";
string FileSaveUri = @"ftp://" + ftpServerIP + AppPath + "/";
//FTP用戶名密碼,就是本機的用戶名密碼
string ftpUser = ftpUserID;
string ftpPassWord = ftpPassword;
Stream requestStream = null;
Stream fileStream = null;
FtpWebResponse uploadResponse = null;//創(chuàng)建FtpWebResponse實例uploadResponse
//Btn_Upload.
if (fu.HasFile)
{
//獲取文件長度
int FileLength = fu.PostedFile.ContentLength;
//限制上傳文件最大不能超過1G
if (FileLength < 1024 * 1024 * 1024)
{
try
{

//格式化為URI
Uri uri = new Uri(FileSaveUri + Path.GetFileName(fu.PostedFile.FileName));
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);//創(chuàng)建FtpWebRequest實例uploadRequest
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//將FtpWebRequest屬性設(shè)置為上傳文件
uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);//認(rèn)證FTP用戶名密碼
requestStream = uploadRequest.GetRequestStream();//獲得用于上傳FTP的流
byte[] buffer = new byte[FileLength];
fileStream = fu.PostedFile.InputStream;//截取FileUpload獲取的文件流,作為上傳FTP的流
fileStream.Read(buffer, 0, FileLength);
requestStream.Write(buffer, 0, FileLength);//將buffer寫入流
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();//返回FTP服務(wù)器響應(yīng),上傳完成
//上傳成功

}
catch (Exception ex)
{
//無法上傳
Response.Write("<script>alert('無法上傳');</script>");
return;
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}//end if #FileLength#
else
{
//上傳文件過大
Response.Write("<script>alert('上傳文件過大,文件不得超過1G');</script>");
return;
}
}//end if #FileUpload.HasFile#
else
{
//未選擇文件
return;
}
}
}
}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 眉山市| 陕西省| 绥芬河市| 和顺县| 丹江口市| 库伦旗| 永济市| 商丘市| 泸西县| 大洼县| 西峡县| 靖宇县| 张家港市| 铁力市| 潜山县| 达州市| 叙永县| 石狮市| 邯郸市| 易门县| 福州市| 八宿县| 诸暨市| 松潘县| 云龙县| 吉木乃县| 汉源县| 团风县| 昭平县| 福泉市| 襄樊市| 铜鼓县| 吉林市| 遵义县| 始兴县| 北宁市| 九江县| 寻乌县| 文成县| 文成县| 吐鲁番市|