文件上传下载

accpt16 2008-12-11 02:01:12
E:\test\File\style.css
以上的地址是文件上传后保存在数据库中的虚拟路径地址。。
怎么样让这个文件显示在页面中,可供用户点击查看或下载啊
...全文
176 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cena_jin 2011-12-26
  • 打赏
  • 举报
回复
学习,学习
zhxingway 2008-12-12
  • 打赏
  • 举报
回复
返回的是一个虚拟路径。。怎么返回楼上说的相对路径呢

虚拟路径就是相对路径啊..
空心兜兜 2008-12-12
  • 打赏
  • 举报
回复
学习下..

djf_1985 2008-12-12
  • 打赏
  • 举报
回复
mark
现在正在做个证件上传功能
lxl_sports 2008-12-12
  • 打赏
  • 举报
回复
你用截取字符串的方法来 把路径截取掉 然后再存放到 数据库 。。。
用一个超链接指向这个地址就可以 进行下载 或者 阅读了。。。
yefengzhixia 2008-12-12
  • 打赏
  • 举报
回复
顶了

刚开了一新的c#群 63242231 希望有志同道合的 朋友加入 更期望高手的加入

有您的加入会更精彩。。。
surewy 2008-12-12
  • 打赏
  • 举报
回复
mark
ChengKing 2008-12-11
  • 打赏
  • 举报
回复
使用一个Grid列表存放下载文件列表. 下面是一些文件上传/下载参考资料:



(一).上传

<INPUT id="WebFile" style="WIDTH: 490px; HEIGHT: 22px" type="file" size="62" name="WebFile" runat="server">
protected System.Web.UI.HtmlControls.HtmlInputFile WebFile;
文件上传参考代码:
/// <summary>
/// 文件上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnUpload_Click(object sender, System.EventArgs e)
{
if(WebFile.PostedFile.FileName=="")
{
Info.Text="请先选择要上传的文件";
return;
}

try
{
char[] spliter = {'\\'};
string [] FileName = WebFile.PostedFile.FileName.Split(spliter,10);

string FullPath = CurrentPath + @"\" + FileName[FileName.Length-1]; //生成完整文件名
WebFile.PostedFile.SaveAs(FullPath); //保存文件
LoadDir(CurrentPath); //重新载入当前目录
}
catch
{
Info.Text="上传文件失败,请与管理员联系";
}
}



(二).下载

1. C#:
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}


2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))

'繁体格式
'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))

'向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream"
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub

Set save file format:
Response.ContentType = "application/ms-excel";
路人乙e 2008-12-11
  • 打赏
  • 举报
回复
必须是网站根目录下的文件
如:http://www.abc.com/file/style.css
xiaoks668 2008-12-11
  • 打赏
  • 举报
回复
上传成功时将路径写进数据库,显示时用repeater读路径就是了啊。
wuyq11 2008-12-11
  • 打赏
  • 举报
回复
读出文件名
HttpContext.Current.Response.Write("<script language='javascript'>window.open('','_bank')</script>");
gengwanshanreally 2008-12-11
  • 打赏
  • 举报
回复
up!
accpt16 2008-12-11
  • 打赏
  • 举报
回复
怎么把文件展示在页面上面?
accpt16 2008-12-11
  • 打赏
  • 举报
回复
发表于:2008-12-11 14:08:272楼 得分:0
上传时保存在库中的地址要存相对的地址,相对于你的站点的
别存绝对的.
==============================================
怎么存相对的地址啊。。
public string FileUpload()
{

HttpFileCollection _files = HttpContext.Current.Request.Files;

string _fileName, _fileExt;

try
{
for (int _iFile = 0; _iFile < _files.Count; _iFile++)
{
System.Web.HttpPostedFile _postedFile = _files[_iFile];

_fileName = System.IO.Path.GetFileName(_postedFile.FileName);

_fileExt = System.IO.Path.GetExtension(_fileName);

string phyPath = HttpContext.Current.Request.MapPath("File\\");

_postedFile.SaveAs(phyPath + _fileName);

return phyPath + _fileName;
}
return null;
}
catch (System.Exception Ex)
{
System.Diagnostics.Debug.Write(Ex);
return "上传错误";

}
}
这是我的上传代码。。返回的是一个虚拟路径。。怎么返回楼上说的相对路径呢?相对路径是不是就是服务器的路径?
accpt16 2008-12-11
  • 打赏
  • 举报
回复
是把数据库里的虚拟地址读出来吗?
lovehongyun 2008-12-11
  • 打赏
  • 举报
回复
上传时保存在库中的地址要存相对的地址,相对于你的站点的

别存绝对的.
jiang_jiajia10 2008-12-11
  • 打赏
  • 举报
回复
http://www.cnblogs.com/liping13599168/archive/2008/09/16/672025.html
你先读出来,然后再参考上面的网址

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧