协慌网

登录 贡献 社区

文件上载 ASP.NET MVC 3.0

我想在上传文件。如何使用 html input file控件上传input file

答案

您不使用文件输入控件。 ASP.NET MVC 中不使用服务器端控件。查看以下博客文章 ,其中说明了如何在 ASP.NET MVC 中实现此目的。

因此,您首先要创建一个包含文件输入的 HTML 表单:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

然后你会有一个控制器来处理上传:

public class HomeController : Controller
{
    // This action renders the form
    public ActionResult Index()
    {
        return View();
    }

    // This action handles the form POST and the upload
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");        
    }
}

转移到byte[] (例如保存到 DB):

using (MemoryStream ms = new MemoryStream()) {
    file.InputStream.CopyTo(ms);
    byte[] array = ms.GetBuffer();
}

要将输入流直接传输到数据库中,而不将其存储在内存中,您可以使用此处此处获取的类并进行一些更改:

public class VarbinaryStream : Stream {
private SqlConnection _Connection;

private string _TableName;
private string _BinaryColumn;
private string _KeyColumn;
private int _KeyValue;

private long _Offset;

private SqlDataReader _SQLReader;
private long _SQLReadPosition;

private bool _AllowedToRead = false;

public VarbinaryStream(
    string ConnectionString,
    string TableName,
    string BinaryColumn,
    string KeyColumn,
    int KeyValue,
    bool AllowRead = false)
{
  // create own connection with the connection string.
  _Connection = new SqlConnection(ConnectionString);

  _TableName = TableName;
  _BinaryColumn = BinaryColumn;
  _KeyColumn = KeyColumn;
  _KeyValue = KeyValue;


  // only query the database for a result if we are going to be reading, otherwise skip.
  _AllowedToRead = AllowRead;
  if (_AllowedToRead == true)
  {
    try
    {
      if (_Connection.State != ConnectionState.Open)
        _Connection.Open();

      SqlCommand cmd = new SqlCommand(
                      @"SELECT TOP 1 [" + _BinaryColumn + @"]
                            FROM [dbo].[" + _TableName + @"]
                            WHERE [" + _KeyColumn + "] = @id",
                  _Connection);

      cmd.Parameters.Add(new SqlParameter("@id", _KeyValue));

      _SQLReader = cmd.ExecuteReader(
          CommandBehavior.SequentialAccess |
          CommandBehavior.SingleResult |
          CommandBehavior.SingleRow |
          CommandBehavior.CloseConnection);

      _SQLReader.Read();
    }
    catch (Exception e)
    {
      // log errors here
    }
  }
}

// this method will be called as part of the Stream ímplementation when we try to write to our VarbinaryStream class.
public override void Write(byte[] buffer, int index, int count)
{
  try
  {
    if (_Connection.State != ConnectionState.Open)
      _Connection.Open();

    if (_Offset == 0)
    {
      // for the first write we just send the bytes to the Column
      SqlCommand cmd = new SqlCommand(
                                  @"UPDATE [dbo].[" + _TableName + @"]
                                            SET [" + _BinaryColumn + @"] = @firstchunk 
                                        WHERE [" + _KeyColumn + "] = @id",
                              _Connection);

      cmd.Parameters.Add(new SqlParameter("@firstchunk", buffer));
      cmd.Parameters.Add(new SqlParameter("@id", _KeyValue));

      cmd.ExecuteNonQuery();

      _Offset = count;
    }
    else
    {
      // for all updates after the first one we use the TSQL command .WRITE() to append the data in the database
      SqlCommand cmd = new SqlCommand(
                              @"UPDATE [dbo].[" + _TableName + @"]
                                        SET [" + _BinaryColumn + @"].WRITE(@chunk, NULL, @length)
                                    WHERE [" + _KeyColumn + "] = @id",
                           _Connection);

      cmd.Parameters.Add(new SqlParameter("@chunk", buffer));
      cmd.Parameters.Add(new SqlParameter("@length", count));
      cmd.Parameters.Add(new SqlParameter("@id", _KeyValue));

      cmd.ExecuteNonQuery();

      _Offset += count;
    }
  }
  catch (Exception e)
  {
    // log errors here
  }
}

// this method will be called as part of the Stream ímplementation when we try to read from our VarbinaryStream class.
public override int Read(byte[] buffer, int offset, int count)
{
  try
  {
    long bytesRead = _SQLReader.GetBytes(0, _SQLReadPosition, buffer, offset, count);
    _SQLReadPosition += bytesRead;
    return (int)bytesRead;
  }
  catch (Exception e)
  {
    // log errors here
  }
  return -1;
}
public override bool CanRead
{
  get { return _AllowedToRead; }
}

protected override void Dispose(bool disposing)
{
  if (_Connection != null)
  {
    if (_Connection.State != ConnectionState.Closed)
      try { _Connection.Close();           }
      catch { }
    _Connection.Dispose();
  }
  base.Dispose(disposing);
}

#region unimplemented methods
public override bool CanSeek
{
  get { return false; }
}

public override bool CanWrite
{
  get { return true; }
}

public override void Flush()
{
  throw new NotImplementedException();
}

public override long Length
{
  get { throw new NotImplementedException(); }
}

public override long Position
{
  get
  {
    throw new NotImplementedException();
  }
  set
  {
    throw new NotImplementedException();
  }
}
public override long Seek(long offset, SeekOrigin origin)
{
  throw new NotImplementedException();
}

public override void SetLength(long value)
{
  throw new NotImplementedException();
}
#endregion unimplemented methods  }

和用法:

using (var filestream = new VarbinaryStream(
                            "Connection_String",
                            "Table_Name",
                            "Varbinary_Column_name",
                            "Key_Column_Name",
                            keyValueId,
                            true))
  {
    postedFile.InputStream.CopyTo(filestream);
  }

传输到 byte [] 的替代方法(用于保存到 DB)。

@Arthur 的方法效果很好,但不能很好地复制,因此 MS Office 文档在从数据库中检索后可能无法打开。 MemoryStream.GetBuffer()可以在 byte [] 的末尾返回额外的空字节,但您可以通过使用 MemoryStream.ToArray()来修复它。但是,我发现这种替代方法适用于所有文件类型:

using (var binaryReader = new BinaryReader(file.InputStream))
{
    byte[] array = binaryReader.ReadBytes(file.ContentLength);
}

这是我的完整代码:

文件类:

public class Document
{
    public int? DocumentID { get; set; }
    public string FileName { get; set; }
    public byte[] Data { get; set; }
    public string ContentType { get; set; }
    public int? ContentLength { get; set; }

    public Document()
    {
        DocumentID = 0;
        FileName = "New File";
        Data = new byte[] { };
        ContentType = "";
        ContentLength = 0;
    }
}

文件下载:

[HttpGet]
public ActionResult GetDocument(int? documentID)
{
    // Get document from database
    var doc = dataLayer.GetDocument(documentID);

    // Convert to ContentDisposition
    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = doc.FileName, 

        // Prompt the user for downloading; set to true if you want 
        // the browser to try to show the file 'inline' (display in-browser
        // without prompting to download file).  Set to false if you 
        // want to always prompt them to download the file.
        Inline = true, 
    };
    Response.AppendHeader("Content-Disposition", cd.ToString());

    // View document
    return File(doc.Data, doc.ContentType);
}

上传文件:

[HttpPost]
public ActionResult GetDocument(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0)
    {
        // Get file info
        var fileName = Path.GetFileName(file.FileName);
        var contentLength = file.ContentLength;
        var contentType = file.ContentType;

        // Get file data
        byte[] data = new byte[] { };
        using (var binaryReader = new BinaryReader(file.InputStream))
        {
            data = binaryReader.ReadBytes(file.ContentLength);
        }

        // Save to database
        Document doc = new Document()
        {
            FileName = fileName,
            Data = data,
            ContentType = contentType,
            ContentLength = contentLength,
        };
        dataLayer.SaveDocument(doc);

        // Show success ...
        return RedirectToAction("Index");
    }
    else
    {
        // Show error ...
        return View("Foo");
    }
}

查看(摘录):

@using (Html.BeginForm("GetDocument", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="Upload File" />
}