使用C#在asp.net中下载文件 [英] Downloading files in asp.net using C#

查看:81
本文介绍了使用C#在asp.net中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中下载文件?
是我上载的内容:
我将文件上载到网站,并将URL保存到这样的数据库中:

How do i download a file in asp.net? here is what i did to upload it: I upload the file into the website and saved the url to it in a database like this:

string CVPath = null;
  if (uploadfiles.HasFile)
  {
    string file = uploadfiles.FileName;
    uploadfiles.PostedFile.SaveAs(Server.MapPath(".") + "//CV//" + file);
    CVPath = "~//ProfileImages//" + file;
    FileName.InnerText = file;
  }
  else
    CVPath = "";

然后将 CVPath保存在数据库中

and then I save the "CVPath" in a database

推荐答案

要下载文件,首先需要将所有内容读取为字符串。

To download a file, first you need to read all the contents to a string.

    MemoryStream ms = new MemoryStream();
    TextWriter tw = new StreamWriter(ms);
    tw.WriteLine("YourString");
    tw.Flush();
    byte[] bytes = ms.ToArray();
    ms.Close();
    Response.Clear();
    Response.ContentType = "application/force-download";
    Response.AddHeader("content-disposition", "attachment; filename=file.txt");
    Response.BinaryWrite(bytes);
    Response.End(); 

这篇关于使用C#在asp.net中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆