我有一个MVC应用程序.我想下载pdf.
这是我观点的一部分:
<p>
<span class="label">Information:</span>
@using (Html.BeginForm("DownloadFile")) { <input type="submit" value="Download"/> }
</p>
Run Code Online (Sandbox Code Playgroud)
这是我的控制器的一部分:
private string FDir_AppData = "~/App_Data/";
public ActionResult DownloadFile()
{
var sDocument = Server.MapPath(FDir_AppData + "MyFile.pdf");
if (!sDocument.StartsWith(FDir_AppData))
{
// Ensure that we are serving file only inside the App_Data folder
// and block requests outside like "../web.config"
throw new HttpException(403, "Forbidden");
}
if (!System.IO.File.Exists(sDocument))
{
return HttpNotFound();
}
return File(sDocument, "application/pdf", Server.UrlEncode(sDocument));
}
Run Code Online (Sandbox Code Playgroud)
我该如何下载特定文件?
问题:我有来自数据库的Id命名的文件.当有人想要下载它时,我需要将其更改为真实姓名.服务器上的文件类似于:http:// localhost:34256/Content/uploads/23.所以例如我的文件名是23但我需要将其更改为textfile1.txt.
我创建了一个包含这些文件列表的局部视图:
@foreach (var item in Model)
{
<a href="/Content/uploads/@item.Id" title="@Html.Encode(item.FileName)">
<img src="@item.IcoSrc" /><br />
@item.FileName
</a>
}
Run Code Online (Sandbox Code Playgroud)
@item.FileName文件的真实姓名在哪里.当有人从这个列表中下载文件时,他得到的文件名为@ item.Id而不是@ item.FileName.我该怎么改变它?
我正在使用MVC3和.NET framevork 4.
任何帮助非常感谢!
我试图从我的REST API返回一个pdf文件,并将ReportController添加到我的控制器集合中,如下所示.
public class ReportController : ApiController
{
public HttpResponseMessage Get(int id)
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
string fileName = id.ToString();
MemoryStream memoryStream = GetStreamFromBlob(fileName);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
其他控制器都可以正常工作,但这是第一个设置为返回HttpResponseMessage而不是可序列化对象或对象集合的控制器.
但是我很难从客户端消费这个.已经尝试了许多版本的代码来执行此操作,但是控制器代码永远不会受到影响,并且似乎很少有成功的方法来调用它.以下是我目前的版本: -
public async Task<string> GetPdfFile(int id)
{
string fileName = string.Format("C:\\Code\\PDF_Client\\{0}.pdf", id);
using (HttpClient proxy = new HttpClient())
{
string url = string.Format("http://localhost:10056/api/report/{0}", id);
HttpResponseMessage reportResponse = await proxy.GetAsync(url); //****
byte[] b = await reportResponse.Content.ReadAsByteArrayAsync();
System.IO.File.WriteAllBytes(fileName, b);
}
return fileName; …Run Code Online (Sandbox Code Playgroud) 我之前已将文件上传到我的数据库,现在我想从我的数据库下载该文件.
谁能告诉我怎么样?我是C#和ASP.NET MVC的新手.
控制器:
public ActionResult Details(string id = null)
{
Assignment assignment = db.Assignments.Find(id);
if (assignment == null)
{
return HttpNotFound();
}
return View(assignment);
}
Run Code Online (Sandbox Code Playgroud)
模型:
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public byte[] FileLocation { get; set; }
Run Code Online (Sandbox Code Playgroud)
视图:
<div class="display-label">
<%: …Run Code Online (Sandbox Code Playgroud) 是否可以File从方法返回对象controller?
目前所有的逻辑都是在控制器中完成的,所以我得到了这个:
public ActionResult Download(Guid id){
//some code to get file name, file stream and file content
return File(fileStream, file.ContentType, file.Name);
}
Run Code Online (Sandbox Code Playgroud)
但我想在控制器中得到的是:
//this is in the controller
public ActionResult Download(Guid id){
var file = GetFile(fileId);
return file;
}
Run Code Online (Sandbox Code Playgroud)
并且这个包含文件本身所有信息的方法应该在服务层:
//this is NOT in the controller
public File GetFile(Guid fileId){
//some logic to get all stuff
return File(fileStream, attachment.ContentType, attachment.Name);
}
Run Code Online (Sandbox Code Playgroud)
但是,我在这种情况下收到消息
“不可调用成员‘文件’不能像方法一样使用。”
我能做到这一点,还是应该忘记这一点并坚持我现在拥有的?
编辑:建议的问题没有回答我的问题!
我可以下载文件,但我希望在我的控制器中有一个返回File类型或其他内容的方法,并且这个方法应该在服务层的另一个项目中。所以这个方法应该以 object() 的形式返回文件,而不是流,而不是文件名或类型。而在控制器中,我只会调用此方法并仅返回此方法返回的内容。
我正在使用MVC4。我已经使用以下简单代码动态生成了Excel文件。我的托管在Azure上。
我创建了一个根路径,然后尝试保存该Excel文件。
问题是当我的ActionResult方法响应返回时,它会提供默认弹出窗口以打开文件,但文件名具有GUID而不是我提供的文件名。
Excel文件生成代码:
Microsoft.Office.Interop.Excel.Application xlApp =新的Microsoft.Office.Interop.Excel.Application();
// ...
//Save
LocalResource resource = RoleEnvironment.GetLocalResource("MyValue");
string tempPath = resource.RootPath + "DemoFile.xls";
return tempPath;
Run Code Online (Sandbox Code Playgroud)
tempPath返回类似的路径C:\AppData\Local\dftmp\Resources\11a2435c-998c-4fe8-aa55-8bb42455b4ca\directory\DemoFile.xls。
下载文件的弹出窗口不会给文件名作为DemoFile它gives some GUID为什么这样呢?
ActionResult 方法代码:
public ActionResult DownloadExcel() {
string path = ExcelGenerationCode(fileName);
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
return new FileStreamResult(s, "application/vnd.ms-excel");
}
Run Code Online (Sandbox Code Playgroud)
也试图给名字属性
public ActionResult DownloadExcel() {
string path = ExcelGenerationCode(fileName);
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
return new FileStreamResult(s, "application/vnd.ms-excel")
{
FileDownloadName = "myexcelFILE1.xls" …Run Code Online (Sandbox Code Playgroud) 我想在页面加载时列出文件夹中的所有文件。所以
为此,我刚刚创建了这样的
HTML代码
<input id="idd" type="file" multiple="true" class="file" data-preview-file-type="text">
Run Code Online (Sandbox Code Playgroud)
脚本
@section scripts{
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '/Home/filesinfolder',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$.each(data, function (index, val) {
$('#idd').append('<li><a href="http://'+ val.Url +'" target="_new">' + val.Url + '</a></li>');
});
},
error: function (xhr, status, err) {
console.log('Response code:' + xhr.status);
console.log('[Error:' + err + '] ' + status);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
控制器方法
public JsonResult filesinfolder()
{
DirectoryInfo salesFTPDirectory = null;
FileInfo[] files = null; …Run Code Online (Sandbox Code Playgroud)