Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将XML文件返回给用户_Asp.net_File - Fatal编程技术网

Asp.net 将XML文件返回给用户

Asp.net 将XML文件返回给用户,asp.net,file,Asp.net,File,我正在创建一个XML文件,当用户转到指定位置时,我希望将其返回给用户。我想到过这样的事情: return new FileStream("questions.xml",FileMode.Open); 这是正确的代码,还是您将如何返回文件?谢谢。当用户下载文件questions.xml时,我可能会遇到修改文件questions.xml的情况。要解决这个问题,我应该怎么做?有没有不需要保存文件的解决方案?@Ryan,您可以直接从内存生成并提供文件,而无需将其保存到磁盘。不要使用Response.

我正在创建一个XML文件,当用户转到指定位置时,我希望将其返回给用户。我想到过这样的事情:

 return new FileStream("questions.xml",FileMode.Open);

这是正确的代码,还是您将如何返回文件?

谢谢。当用户下载文件questions.xml时,我可能会遇到修改文件questions.xml的情况。要解决这个问题,我应该怎么做?有没有不需要保存文件的解决方案?@Ryan,您可以直接从内存生成并提供文件,而无需将其保存到磁盘。不要使用
Response.WriteFile
,而是使用
Response.Write
并将XML内容传递给
Response.OutputStream
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Clear();
    var file = Server.MapPath("~/questions.xml");
    Response.WriteFile(file);
    Response.ContentType = "text/xml";
    Response.AddHeader("Content-Disposition", "attachment; filename=questions.xml");
}