这就是我正在使用的......你应该做一些改变以适应你的要求。
如何将 id 和 pdf 文件都传递给控制器?
看法:
@using (Html.BeginForm("Add", "Archivos",
FormMethod.Post, new { id = "attachment", enctype = "multipart/form-data", encoding = "multipart/form-data" }))
{
@Html.HiddenFor(x => Model.UserID)
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" value="Guardar"/>
}
控制器:
[HttpPost]
public ActionResult Add(HttpPostedFileBase uploadFile, int UserID)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
//instance the user.. i.e "User1"
byte[] tempFile = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);
User1.file.Content = tempFile;
User1.file.Save();
}
return RedirectToAction("Index");
}
然后我怎样才能下载pdf?
控制器:
public ActionResult Get(int UserID)
{
var User1 = new User {UserID = UserID };
User1.LoadFile();
//If file exists....
MemoryStream ms = new MemoryStream(User1.File.Content, 0, 0, true, true);
Response.ContentType = User1.File.Type;
Response.AddHeader("content-disposition", "attachment;filename=" + User1.File.Name);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, User1.File.Type);
}
看法:
@Html.ActionLink("PDF", "Get", "Files", new { UserID = Model.UserID }, new { @class = "pdf-icon-l", title="Open PDF document" })
以及如何将 pdf 存储在 SQL Server 的表客户端中?
数据库表:
CREATE TABLE [dbo].[FileTableName] (
[UserID] int NOT NULL,
[Name] varchar(256) NOT NULL,
[Type] varchar(256) NOT NULL,
[Length] int NOT NULL,
[Content] varchar(max) NOT NULL) // option: varbinary(max)
要存储我正在使用存储过程的文件
模型:
public class File
{
public string Name { get; set; }
public string Type { get; set; }
public long Length { get; set; }
public byte[] Content { get; set; }
}
public class User
{
public int UserID {get; set;}
public string name {get; set;}
/**/
public file file {get; set;}
/**/
public void SaveFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("SavePDFFile", MyConnectionManager.Connection))
{
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.UserID));
_sqlCommand.Parameters.Add(new SqlParameter("@Name", this.Name));
_sqlCommand.Parameters.Add(new SqlParameter("@Type", this.Type));
_sqlCommand.Parameters.Add(new SqlParameter("@Length", this.Length));
_sqlCommand.Parameters.Add(new SqlParameter("@Content", this.Content));
_dataReader = _sqlCommand.ExecuteReader();
_dataReader.Close();
}
}
}
public void LoadFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("GetPDFFIle", MyConnectionManager.Connection))
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.IDEnsayo));
_dataReader = _sqlCommand.ExecuteReader();
if (_dataReader.HasRows)
{
_dataReader.Read();
this.File.Name = (string)_dataReader["Name"];
this.File.Type = (string)_dataReader["Type"];
this.File.Length = (int)_dataReader["Length"];
this.File.Content = (byte[])_dataReader["Content"];
}
_dataReader.Close();
}
}
}
}