【问题标题】:System.IO.File Non-invocable member 'File' cannot be used like a methodSystem.IO.File 不可调用的成员“文件”不能像方法一样使用
【发布时间】:2019-10-15 06:19:50
【问题描述】:

你好 Stackoverflow 社区,

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

不可调用的成员“文件”不能像方法一样使用

这是我的数据库表:

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);
    }
 }
}

当我尝试将 File 作为文本文件返回时,我的控制器上出现此错误。我要做的就是读取文件(保存为数据库中的字节数组)并将其返回给我的 Angular 应用程序。我尝试了多种在线发布的解决方案,但没有一个对我有帮助。我将不胜感激从该博客获得的任何帮助。我正在使用 .NET Framework 4.6.1 和 MVC 5.2.7。谢谢

【问题讨论】:

  • ApiController 没有 File 方法。您正在尝试使用 System.IO.File 类型作为方法。
  • 类型不是方法。类型通常包含方法、字段和/或属性。由于您发布的代码中的任何地方都没有“文件”方法,您尝试调用哪个方法?
  • 从表面上看,OP 打算从 MVC 的 Controller 类中调用 File 方法。
  • 不,你不是。您的控制器继承自 System.Web.Http.ApiController (Web API),而不是 System.Web.Mvc.Controller (MVC)。
  • stackoverflow.com/questions/9541351/… 是您要找的。​​span>

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

我创建了一个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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2016-05-01
    • 2016-04-11
    • 2018-05-31
    相关资源
    最近更新 更多