Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - 'System.Web.HttpPostedFileBase' does not contain a definition for 'HasFile' and no extension method 'HasFile'

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...