C# System.IO.File非发票成员';文件';不能像方法一样使用

C# System.IO.File非发票成员';文件';不能像方法一样使用,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,大家好,我的社区 我从Web API控制器下载文件时收到以下错误消息 不可开票的成员“File”不能像方法一样使用 这是我的数据库表: CREATE TABLE [cafe].[t06_03_02_jobAttachments]( [ID] [int] IDENTITY(1,1) NOT NULL, [jobAttachmentId] [int] NOT NULL, [jobId] [int] NOT NULL, [file] [varbinary](max) NOT NULL, [name]

大家好,我的社区

我从Web API控制器下载文件时收到以下错误消息

不可开票的成员“File”不能像方法一样使用

这是我的数据库表:

CREATE TABLE [cafe].[t06_03_02_jobAttachments](
[ID] [int] IDENTITY(1,1) NOT NULL,
[jobAttachmentId] [int] NOT NULL,
[jobId] [int] NOT NULL,
[file] [varbinary](max) NOT NULL,
[name] [varchar](100) NOT NULL,
[fileExtension] [varchar](10) NOT NULL,
[contentType] [varchar](50) NOT NULL,
[fileSize] [int] NOT NULL
) ON [PRIMARY]

这是我的数据模型:

namespace api.Models.Jobs
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

[Table("cafe.t06_03_02_jobAttachments")]
public partial class t06_03_02_jobAttachments
{
    public int ID { get; set; }

    public int jobAttachmentId { get; set; }

    public int jobId { get; set; }

    [Required]
    public byte[] file { get; set; }

    [Required]
    [StringLength(100)]
    public string name { get; set; }

    [Required]
    [StringLength(10)]
    public string fileExtension { get; set; }

    [Required]
    [StringLength(50)]
    public string contentType { get; set; }

    public int fileSize { get; set; }

 }
}
这是我的控制器代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using api.Models;
using api.Models.Jobs;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Web.Mvc;

namespace api.Controllers.jobs
{
 public class JobsController : ApiController
 {
    [HttpGet]
    [ResponseType(typeof(t06_03_02_jobAttachments))]
    public async Task<IHttpActionResult> Gett06_03_02_jobAttachments(int id)
    {
      t06_03_02_jobAttachments t06_03_02_jobAttachments = await db.t06_03_02_jobAttachments.FindAsync(id);
        if (t06_03_02_jobAttachments == null)
        {
            return NotFound();
        }
        return File(t06_03_02_jobAttachments.file, t06_03_02_jobAttachments.contentType, t06_03_02_jobAttachments.name);
    }
 }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Threading.Tasks;
使用System.Web.Http;
使用System.Web.Http.Description;
使用api.Models;
使用api.Models.Jobs;
使用System.Data.SqlClient;
使用系统文本;
使用System.IO;
使用System.Web.Mvc;
命名空间api.Controllers.jobs
{
公共类作业控制器:ApiController
{
[HttpGet]
[响应类型(类型(t06\U 03\U 02\U作业附件))]
公共异步任务Gett06\u 03\u 02\u作业附件(int-id)
{
t06_03_02_作业附件t06_03_02_作业附件=wait db.t06_03_02_作业附件.FindAsync(id);
如果(t06\U 03\U 02\U作业附件==null)
{
返回NotFound();
}
返回文件(t06_03_02_jobAttachments.File、t06_03_02_jobAttachments.contentType、t06_03_02_jobAttachments.name);
}
}
}

当我尝试将文件作为文本文件返回时,我在控制器上遇到此错误。我要做的就是读取文件(在数据库中保存为字节数组)并将其返回给我的Angular应用程序。我尝试了多种在线发布的解决方案,但没有一种对我有所帮助。我将非常感谢从这个博客得到的任何帮助。我正在使用.NETFramework4.6.1和MVC5.2.7。谢谢

我制作了一个
FileResult
类,您可以使用它返回二进制内容

public class FileResult : IHttpActionResult
{
    private byte[] _content;
    private string _contentType;
    public FileResult(byte[] content, string contentType)
    {
        _content = content;
        _contentType = contentType;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        if (cancellationToken.IsCancellationRequested)
            return null;

        // per https://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api do not wrap this in a using block
        System.IO.MemoryStream stream = new System.IO.MemoryStream(_content);
        stream.Seek(0, System.IO.SeekOrigin.Begin);
        HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
        response.Content = new StreamContent(stream);
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(_contentType);
        return Task.FromResult(response);
    }
}
公共类文件结果:IHttpActionResult
{
专用字节[]_内容;
私有字符串_contentType;
公共文件结果(字节[]内容,字符串contentType)
{
_内容=内容;
_contentType=contentType;
}
公共任务执行同步(CancellationToken CancellationToken)
{
if(cancellationToken.IsCancellationRequested)
返回null;
//https://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api 不要将其包装在using块中
System.IO.MemoryStream stream=新的System.IO.MemoryStream(_内容);
Seek(0,System.IO.SeekOrigin.Begin);
HttpResponseMessage response=新的HttpResponseMessage(System.Net.HttpStatusCode.OK);
response.Content=新的流内容(stream);
response.Content.Headers.ContentType=新系统.Net.Http.Headers.MediaTypeHeaderValue(_ContentType);
返回Task.FromResult(响应);
}
}

ApiController
没有
文件
方法。您正试图使用
System.IO.File
类型作为方法。类型不是方法。类型通常包含方法、字段和/或属性。由于您发布的代码中没有任何“File”方法,您尝试调用哪种方法?从外观上看,OP打算从MVC的
Controller
类调用
File
方法。不,您没有。您的控制器继承自
System.Web.Http.ApiController
(Web API),而不是
System.Web.Mvc.controller
(Mvc)。这就是你要找的。