.NET实现FTP上传功能错误
我的前台代码如下:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeBehind= "FtpUpload.aspx.cs " Inherits= "MyInterface.UseFtp.FtpUpload " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<input id= "File1 " runat= "server " type= "file " />
<br />
<asp:Button ID= "txtUpload " runat= "server " OnClick= "txtUpload_Click " Text= "上传 " />
<asp:Label ID= "lblP " runat= "server "> </asp:Label> </div>
</form>
</body>
</html>
主要是一个上传控件用来得到用户上传的文件路径.
一个提交按钮
后台代码如下:
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.Net;
using System.IO;
namespace MyInterface.UseFtp
{
public partial class FtpUpload : System.Web.UI.Page
{
//ftp服务器IP
string ftpServerIP = ConfigurationSettings.AppSettings[ "ftpServerIP "];
//ftp用户名
string ftpUserID = ConfigurationSettings.AppSettings[ "ftpUserID "];
//ftp用户密码
string ftpPassword = ConfigurationSettings.AppSettings[ "ftpPassword "];
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 利用FTP上传
/// by minjiang 07-09-05
/// </summary>
/// <param name= "filename "> 上传的文件名 </param>
private void mFtpUpload(string filename )
{
string strfileoldname;//图片保存后名称
//为文件命名,然后保存
string fileExtension;
fileExtension = System.IO.Path.GetExtension(filename);
Random ra = new Random();
strfileoldname = ra.Next(100000, 999999).ToString() + fileExtension;
FileInfo fileInf = new FileInfo(filename);
//string uri = "ftp:// " + ftpServerIP + "/ " + fileInf.Name;
string uri = "ftp:// " + ftpServerIP + "/ " + strfileoldname ;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
//重命名文件的新名称 by minjiang 07-09-05
reqFTP.RenameTo = strfileoldname;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//设置上传进度变量
double dProgess = 1.00;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
FtpWebResponse resFTP = null ;
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
Response.Write( "上传成功,文件路径: "+uri );
}
catch (Exception ex)
{
string err = ex .Message .ToString ();
if (resFTP != null)
{
string sResFTP = resFTP.StatusCode.ToString();
}
Response.Write(err);
}
}
//执行上传功能
protected void txtUpload_Click(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
//要上传的文件名
string fileName = this.File1.PostedFile.FileName;
this.mFtpUpload(fileName );
}
}
}
测试页面如下:
现在上传文件报错:
Could not find file 'E:\MyFile\挪威森林.mp3 '.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not find file 'E:\MyFile\挪威森林.mp3 '.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[FileNotFoundException: Could not find file 'E:\MyFile\挪威森林.mp3 '.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +2014381
System.IO.FileInfo.get_Length() +2706588
MyInterface.UseFtp.FtpUpload.mFtpUpload(String filename) in D:\xwtxzj\MyInterface\MyInterface\UseFtp\FtpUpload.aspx.cs:65
MyInterface.UseFtp.FtpUpload.txtUpload_Click(Object sender, EventArgs e) in D:\xwtxzj\MyInterface\MyInterface\UseFtp\FtpUpload.aspx.cs:120
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
希望朋友们帮我解答一下,谢谢了.
[解决办法]
对这个路径是否有足够的权限访问? 'E:\MyFile\挪威森林.mp3 '
[解决办法]
服务器上没有这个文件自然会出现这个错误啊。 'E:\MyFile\挪威森林.mp3 '是服务器的路径。
[解决办法]
上传到ftp只能先在web服务器file.saveas() ,然后服务器在读入流。然后上传至ftp服务器