文件下载使用.net mvc [英] File Downloading using .net mvc

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

问题描述



我有从服务器下载PDF文件的操作。在本地主机上的任何方式它都能正常工作。但是在主机之后,它无法正常工作。之后,我通过互联网搜索,6小时用Google搜索。但还没有回答。



---------------------------

使用此代码段的本地主机工作

Hi,
I have a action to download a PDF files from server. Any way at the local host it works correctly. but after host, it does not work correctly. any way after that i search over the internet, 6 hours googled. but no answer yet.

---------------------------
At the local host work using this code segment

/*launch File*/   
System.Diagnostics.Process.Start(targetPath + fileName);



完整路径是targetpath + fileName。



请帮我找一个解决方案。


full path is targetpath+fileName.

Please help me to find a solution.

推荐答案

确实是每个绅士的评论是正确的。系统诊断库中的Process.start是打开文件或启动进程(exe,bat文件)的控制台或客户端方式,这在Web服务器上不起作用,因为浏览到您站点的人不在Web上服务器。它可能适用于localhost,因为localhost是您所在的计算机。如果您真的在编写MVC WEB 应用程序而不是客户端应用程序。你需要在你的控制器中创建一个方法来返回一个fileresult并从一个视图动作中调用它。



Indeed the each gentlemen's comments are correct. Process.start from the system diagnostics libary is the console or client way to open a file or start a process(exe, bat file), that doesn't work on a web server because people browsing to your site aren't on the web server. It may work on localhost because localhost IS the computer you are on. If you are truly writing an MVC WEB app and not a client app. You need to make a method in your controller that returns a fileresult and call that from a view action.

public FileResult DownloadFile(){
     fullpath = Server.MapPath("relative path of where file is") + filename;
     return File(fullpath, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(fullpath));
}


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

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