- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 ASP.NET MVC 4
应用程序,在许多 View 中用户可以上传我保存在服务器上的文件。除此之外,我还有一个单独的实体,用于保存上传文件的不同数据,例如:
string fileExtension = System.IO.Path.GetExtension(uploadFile.FileName);
string uniqueGuid = Helper.GetUniqueName(Server.MapPath("~/Content/Files"));
string newFileName = uniqueGuid + fileExtension;
这只是为了获得印象。我对从用户上传的每个文件执行许多操作,完成后我在数据库中使用信息创建新记录并将文件保存在服务器上。
我的解决方案有两个项目 - 一个是 MVC 4
应用程序,另一个用作数据访问层。在使用 Entity Framework 5
的地方,我实现了 Repository pattern
和 UnitOfWork
。
我想做的是在我的存储库中创建新方法。像这样的东西:
public bool ManageFile(HttpPostedFileBase file );
并从我的 Controller 中调用此方法,而不是同时编写相同的逻辑。
我的 Controller 中接受HttpPostedFileBase 文件
的标准Action
如下所示:
public ActionResult Edit(SomeViewModel model, HttpPostedFileBase uploadFile)
在这个 Action 中我想调用:
unitOfWork.SomeModelRepository.ManageFile(uploadFile);
问题是我的数据访问层项目无法识别 HttpPostedFileBase
。也许我可以添加一些对该项目的引用,我不确定,因为我在使用 Server.MapPath
时遇到了一些问题,例如在 Action
之外,但即使我可以引用它我仍然不确定它是否是更好的方法。毕竟我的数据访问层只是这个 - 一个抽象层我不认为 HttpPostedFileBase
在那里有一席之地。
在我的 Controller Action
中,我尝试将HttpPostedFileBase uploadFile
解析为File uploadFile
,但我做不到。但基本上我需要的是能够执行与文件相同的操作 - 获取扩展名、获取名称等等...那么如何将 HttpPostedFileBase uploadFile
传递给我的 DAL
项目?
最佳答案
HttpPostedFileBase在 System.Web 程序集中,您可以从 DAL 项目中引用它。
另一种选择,如果你想将你的 ManageFile() 方法与 HttpPostedFileBase 分离,只需传入文件的 MemoryStream。以及任何其他必需的参数:
public bool ManageFile(Stream file, string fileName)
{
}
我认为单元测试更容易,如果允许调用者提供路径信息,则可以使用不同的命名方案重用 ManageFile()。
也是为了它的值(value),如果
string uniqueGuid = Helper.GetUniqueName(Server.MapPath("~/Content/Files"));
只是根据现有文件生成一个唯一的Guid,你可以跳过这个逻辑。 Guid's will pretty much always be unique ,所以做 Guid.NewGuid() 就足够了。
public bool ManageFile(Stream file, string fileName)
{
string extension = System.IO.Path.GetExtension(fileName);
string newFileName = Guid.NewGuid() + extension;
}
编辑添加:
以下是我在项目中构建此类内容的方式。我有一个 IFileRepository 接口(interface):
interface IFileRepository
{
SaveFile(Stream stream, string fileName);
}
然后我根据要保存文件的位置创建一个具体类型
public AzureFileRepository : IFileRepository
{
public void SaveFile(Stream stream, string fileName)
{
...
CloudBlob.UploadFromStream(stream);
...
}
}
或
public DiskFileRepository : IFileRepository
{
public void SaveFile(Stream stream, string fileName)
{
File file = // create a File() object and save the stream
}
}
现在 Controller 对文件存储库一无所知。它只是调用 SaveFile() 并且奇迹发生了。
关于c# - 如何将 HttpPostedFileBase 文件作为另一个项目的方法的参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526463/
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
我有一个单元测试,我正在模拟 HttpPostedFileBase 文件并将它们放在列表中。单元测试然后像这样调用邮件服务(不相关的参数,如 to、from、subject 被忽略) [Test] p
我正在尝试使用 MVC 实现上传附件功能。我实际执行上传/保存附件的方法需要 HttpPostedFileBase 类型。 public virtual string Upload(HttpPoste
我有 Kendo ThemeBuilder,在 MVC C# 中创建一个主题,因为我有一个模型,其中包含从 themebuilder 返回的 css body 要将它保存到 css 文件,我可以通过创
我在 HttpPostedFileBase 上遇到错误: The type or namespace name 'HttpPostedFileBase'could not be found(are y
我在带有 CSLA 的分布式环境中使用 MVC .NET,我可以从我的网络层之一(例如 Website.MVC)引用 HttpPostedFileBase,但我不能从单独的层(我们称之为 OtherL
在我的Create View中我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我正在使用 ASP.NET MVC,并且我有一个上传文件的操作。文件正在正确上传。但我想要图像的宽度和高度。我想我需要先将 HttpPostedFileBase 转换为 Image 然后继续。我该怎么
在我的Create View 我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我的客户端javascript代码是 function SendMail() { debugger; var decpr = tinyMCE.get('taskdesc
我正在开发一个 ASP.NET MVC 4 应用程序,在许多 View 中用户可以上传我保存在服务器上的文件。除此之外,我还有一个单独的实体,用于保存上传文件的不同数据,例如: string fi
如果我禁用客户端验证 并尝试上传一个大约 11 MB 的文件,将文件设置为 HttpPostedFileBase memeber [ValidateFile] public Htt
我正在使用 C# 开发一个 ASP.NET MVC 项目,试图将文档从表单上传到我的数据库。我目前在我的 Controller 中,我已将文件导入为 HttpPostedFileBase,并且必须将其
有没有一种简单的方法可以从 HttpPostedFileBase 中获取 FileInfo 对象?我意识到我可以保存文件,然后执行类似 DirectoryInfo.GetFiles 的操作,然后循环遍
那么,场景是这样的:用户上传文件,我的代码将此文件转换为字节数组,然后将该数组传递给外部 API。这很好用。 问题是这个文件包含了像æ,ø,å这样的特殊字符,当byte[]再次转换成字符时,这些字符被
我无法弄清楚我做错了什么,但由于某种原因 HttpPostedFileBase 总是返回 null。当我尝试在服务器端上传文件时,我继续使用 HttpPosterFileBase 获取 null:这是
我正在尝试创建一种方法,我可以在其中传递图像文件并检索该图像文件的字节,以便稍后我可以将字节存储在数据库中。这是我的方法代码。 private byte[] GetImageBytes(HttpPos
我正在尝试在 ASP.NET MVC 中上传单个 .csv 文件。在我的 .ascx 文件中,我有:  
我必须使用具有如下方法的网络服务: SubmitUser(UserReg user, HttpPostedFileBase image) { // webservice side process
我是一名优秀的程序员,十分优秀!