mvc ftp上传文件求助

不喜欢等待 不喜欢等待 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在最近项目中的一个ftp使用<input type="file">上传文件 在上传的时候我用HttpPostedFileBase对象 想得到文件的客服端路径,就像是asp.net中的fileupload控件一样 能直接的到 ,现在的话 HttpPostedFileBase里面没有的文件在客服端路径的属性
所以用HttpPostedFileBase 的FileName属性 ,但是在ie先却能得到他的路径 在chrom或者是别的浏览器却的不到所以老是郁闷
,求大家指教,想下面的这这个方法 我该怎么传参,小弟想的是应该传文件在客服端的路径

private void Upload(string filename)
{
string ftpServerIP = "ftp地址";
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp地址" + fileInf.Name;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/web/" + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential("用户名", "密码");
// 默认为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;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
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();
Console.Write("上传成功");
}
catch (Exception ex)
{

Console.Write(ex.Message);
}
}

我在Controller里是这样调用的

public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection fc)
{
HttpPostedFileBase file = Request.Files[0];
Upload(file.FileName);
return View();
}

页面代码如下:

<form method="post" enctype="multipart/form-data">
<input type="file" name="fileupdate" />
<input type="submit" />
</form>


以上为在下遇见,还望高手能指教下 我这是上传大文件 所以用到了 流 还望指教 或给出代码 ,万分感激
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
不喜欢等待
.NET社区 62,271 成员 668,968 内容