本文介绍了“System.Web.HttpPostedFileBase'不包含'HasFile',没有扩展方法'HasFile”的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的.在C#中使用VS2010,ASP.NET 4.0,MVC3用在SQL Server 2008R2 ADO.NET。我收到以下错误消息...

System.Web.HttpPostedFileBase'不包含'HasFile',没有扩展方法'HasFile接受型System.Web.HttpPostedFileBase第一个参数的定义可以找到(是否缺少using指令或程序集引用?)

我通过搜索Stackflow,有一些事情,包括System.Web.Abstractions。我包括在此,我仍然得到错误。
在此先感谢如果有人能告诉我的解决方案。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用System.IO;
使用System.Data.SqlClient的;
使用System.Web.Helpers;命名空间MvcApplication1.Controllers
{
  公共类myController的:控制器
  {    //
    // 拿我的/    公众的ActionResult指数()
    {
        的foreach(在Request.Files串上传)
        {
            如果(Request.Files [上传] .HasFile()!)继续;            串mime类型= Request.Files [上传] .ContentType;
            流FILESTREAM = Request.Files [上传] .InputStream;
            字符串文件名= Path.GetFileName(Request.Files [上传] .FileName);
            INT文件长度= Request.Files [上传] .ContentLength;
            字节[] = FILEDATA新的字节[文件长度]
            fileStream.Read(FILEDATA,0,文件长度);            常量字符串连接= @服务器= \\ SQLEx preSS;数据库= FileTest; Trusted_Connection = TRUE;;
            使用(VAR康恩=新的SqlConnection(连接))
            {
                VAR QRY =INSERT INTO的FileStore(FileContent,Mime类型,文件名)VALUES(@FileContent,@MimeType,@FileName);
                VAR CMD =新的SqlCommand(QRY,康涅狄格州);
                cmd.Parameters.AddWithValue(@ FileContentFILEDATA);
                cmd.Parameters.AddWithValue(@ Mime类型,mime类型);
                cmd.Parameters.AddWithValue(@文件名,文件名);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        返回查看();
    }    公共FileContentResult的GetFile(INT ID)
    {
        SqlDataReader的RDR;字节[] fileContent = NULL;
        串mime类型=;字符串文件名=;
        常量字符串连接= @服务器= \\ SQLEx preSS;数据库= FileTest; Trusted_Connection = TRUE;;        使用(VAR康恩=新的SqlConnection(连接))
        {
            VAR QRY =SELECT FileContent,Mime类型,文件名FROM WHERE的FileStore ID = @ID;
            VAR CMD =新的SqlCommand(QRY,康涅狄格州);
            cmd.Parameters.AddWithValue(@ ID中,ID);
            conn.Open();
            RDR = cmd.ExecuteReader();
            如果(rdr.HasRows)
            {
                rdr.Read();
                fileContent =(字节[])RDR [FileContent];
                mime类型= RDR [Mime类型]的ToString()。
                文件名= RDR [文件名]的ToString()。
            }
        }
        返回文件(fileContent,mime类型,文件名);
    }
 }
 }

在助手文件夹我的助手类。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间MvcApplication1.Models
{
公共静态类助手
{    公共静态布尔HasFile(此HttpPostedFileBase文件)
    {
        回报(文件= NULL&放大器;!&安培; file.ContentLength大于0)?真假;
    }
}


解决方案

您需要一个using语句添加到您的 myController的 code文件,因为这是当你想使用的扩展方法的需要(它需要的范围的):

 使用MvcApplication1.Models;

I am following http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files. Using VS2010, ASP.NET 4.0, MVC3 in C# with ADO.NET in SQL Server 2008R2. I am getting the following error message...

'System.Web.HttpPostedFileBase' does not contain a definition for 'HasFile' and no extension method 'HasFile' accepting a first argument of type 'System.Web.HttpPostedFileBase' could be found (are you missing a using directive or an assembly reference?)

I searched through Stackflow, there was something about including System.Web.Abstractions. I included this and I still getting the error. Thanks in advance if anyone can tell me the solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data.SqlClient;
using System.Web.Helpers;

namespace MvcApplication1.Controllers
{
  public class MyController : Controller 
  {

    //
    // GET: /My/

    public ActionResult Index()
    {
        foreach (string upload in Request.Files)
        {
            if (!Request.Files[upload].HasFile()) continue;

            string mimeType = Request.Files[upload].ContentType;
            Stream fileStream = Request.Files[upload].InputStream;
            string fileName = Path.GetFileName(Request.Files[upload].FileName);
            int fileLength = Request.Files[upload].ContentLength;
            byte[] fileData = new byte[fileLength];
            fileStream.Read(fileData, 0, fileLength);

            const string connect = @"Server=.\SQLExpress;Database=FileTest;Trusted_Connection=True;";
            using (var conn = new SqlConnection(connect))
            {
                var qry = "INSERT INTO FileStore (FileContent, MimeType, FileName) VALUES (@FileContent, @MimeType, @FileName)";
                var cmd = new SqlCommand(qry, conn);
                cmd.Parameters.AddWithValue("@FileContent", fileData);
                cmd.Parameters.AddWithValue("@MimeType", mimeType);
                cmd.Parameters.AddWithValue("@FileName", fileName);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        return View();
    }

    public FileContentResult GetFile(int id)
    {
        SqlDataReader rdr; byte[] fileContent = null;
        string mimeType = ""; string fileName = "";
        const string connect = @"Server=.\SQLExpress;Database=FileTest;Trusted_Connection=True;";

        using (var conn = new SqlConnection(connect))
        {
            var qry = "SELECT FileContent, MimeType, FileName FROM FileStore WHERE ID = @ID";
            var cmd = new SqlCommand(qry, conn);
            cmd.Parameters.AddWithValue("@ID", id);
            conn.Open();
            rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                rdr.Read();
                fileContent = (byte[])rdr["FileContent"];
                mimeType = rdr["MimeType"].ToString();
                fileName = rdr["FileName"].ToString();
            }
        }
        return File(fileContent, mimeType, fileName);
    }
 }
 }

in the Helpers folder I have the Helper class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
public static class Helper
{

    public static bool HasFile(this HttpPostedFileBase file)
    {
        return (file != null && file.ContentLength > 0) ? true : false;
    }
}
解决方案

You need to add a using statement to your MyController code file, as that is what is required when you want to use an extension method (it needs to be in scope):

using MvcApplication1.Models;

这篇关于“System.Web.HttpPostedFileBase'不包含'HasFile',没有扩展方法'HasFile”的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:49