当用户上传jpg/gif/bmp图像时,我希望将此图像转换为png图像,然后转换为base64字符串.
我一直试图让这个工作,但我真的打了一堵砖墙,有人可以帮我吗?
我目前没有图像转换的代码如下:
public ActionResult UploadToBase64String(HttpPostedFileBase file)
{
var binaryData = new Byte[file.InputStream.Length];
file.InputStream.Read(binaryData, 0, (int) file.InputStream.Length);
file.InputStream.Seek(0, SeekOrigin.Begin);
file.InputStream.Close();
string base64String = Convert.ToBase64String(binaryData, 0, binaryData.Length);
...
}
Run Code Online (Sandbox Code Playgroud) 我正在使用C#编写ASP.NET MVC项目,试图将文档从表单上传到我的数据库.我目前在我的控制器中,我已将文件导入为HttpPostedFileBase,并且必须将其转换为Stream类型.我怎么能实现这个目标?
提前致谢!
HttpPostedFileBase document = form.DocumentFile;
Stream documentConverted = ?;
Run Code Online (Sandbox Code Playgroud) 我遇到过这样的情况:我在 MVC 代码中获取 httppostedfilebase 类型的图像。我的 SQL 数据库中有一个相应的图像类型列。
我需要知道如何将此 httppostedfilebase 类型转换/保存为数据库中的图像。
嗯,这很奇怪.我正在做我在网上大量发现的一切,但仍然无效.我想上传一个文件,所以在View I中:
@using (Html.BeginForm("Import", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" />
<input type="submit" value="Importa" />
}
Run Code Online (Sandbox Code Playgroud)
这是Home/Import:
[AcceptVerbs(HttpVerbs.Post)] // or [HttpPost], nothing changes
public ActionResult Import(HttpPostedFileBase uploadFile)
{
Debug.WriteLine("Inside Import");
if (uploadFile == null)
Debug.WriteLine("null");
// Verify that the user selected a file
if (uploadFile != null && uploadFile.ContentLength > 0)
{
Debug.WriteLine("Valid File");
...
}
}
Run Code Online (Sandbox Code Playgroud)
它总是打印Inside Import + null.所以我确定我调用了正确的Controller Action,但它总是收到一个null HttpPostedFileBase.
有没有人有建议或已经找到(并解决)这种问题?谢谢!
我在asp .net mvc 3中有一个视图模型
IEnumerable<HttpPostedFileBase> files
Run Code Online (Sandbox Code Playgroud)
在视图中,我有一个for循环,为这些文件创建9个输入标记.
我想在服务器端执行检查以确保至少上传3个文件.
我试着提出一个条件
if(files.Count() > 2) { // code here }
Run Code Online (Sandbox Code Playgroud)
但是,它返回9,因为它也计算null元素.
我可以考虑自己实施一个计数器如下:
int count = 0;
@foreach(var file in files) {
if(file != null && file.ContentLength > 0) {
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
这是执行此操作的最佳方式还是asp .net mvc中已有功能.
我正在尝试将文件上传到目录.以下代码对我有用
[HttpPost]
public ActionResult Index(HttpPostedFileBase uploadFile)
{
//string path = @"C:\Users\thomas\Desktop";
if (uploadFile != null)
{
string filePath = Path.Combine(Server.MapPath("/files"), Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form action="/Post/Index" method="post" enctype="multipart/form-data">
<label for="uploadFile">Upload file: </label>
<input name="uploadFile" id="uploadFile" type="file" />
<input value="uploadFile" type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试在一个函数中实现它,我创建一个消息,该消息由包含消息和项类的模型创建.当我提交表单时,模型将传递给我的savecontroller但在我的参数控制器中该文件为null.
HTML PAGE
@model GeoCitytroopers.Models.MessageItemModel
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Event</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Message.MessagePicture)
</div>
<div>
<label for="uploadFile">Upload file: …Run Code Online (Sandbox Code Playgroud) 有人介意指导我,如何将文件保存到我的数据库中,并在可能的情况下检索它,我还是这个 C# 和 MVC 4 的新手。我的数据库分配包含一个属性调用 FileLocation,它是 varBinary (MAX)。
public partial class Assignment
{
public Assignment()
{
this.CourseAvailables = new HashSet<CourseAvailable>();
}
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public byte[] FileLocation { get; set; }
public virtual ICollection<CourseAvailable> CourseAvailables { get; set; } …Run Code Online (Sandbox Code Playgroud) 我之前已将文件上传到我的数据库,现在我想从我的数据库下载该文件.
谁能告诉我怎么样?我是C#和ASP.NET MVC的新手.
控制器:
public ActionResult Details(string id = null)
{
Assignment assignment = db.Assignments.Find(id);
if (assignment == null)
{
return HttpNotFound();
}
return View(assignment);
}
Run Code Online (Sandbox Code Playgroud)
模型:
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public byte[] FileLocation { get; set; }
Run Code Online (Sandbox Code Playgroud)
视图:
<div class="display-label">
<%: …Run Code Online (Sandbox Code Playgroud) 我已经为此检查了各种答案,但没有一个对我有用,我在 .cshtml 中的代码如下:
<input type="file" name="PostedNRLFile" />
Run Code Online (Sandbox Code Playgroud)
然后在控制器中我有
public JsonResult SaveRecord(NewAuditLetterViewModel viewModel, FormCollection all, string hvalue, HttpPostedFileBase PostedNRLFile)
Run Code Online (Sandbox Code Playgroud)
始终为空,请帮助我。
我已经尝试过一些事情,比如在 viewmodel 中创建一个属性,这也是空的。也用于 new { enctype = "multipart/form-data", id = "documentForm" }我的 beginform 标签。还检查了他们的源代码中只有一个标签。
我正在使用文件,我真的不明白如何使它工作.
我有一个位于服务器上的Web应用程序(c#),用户使用此应用程序从文件上传一些信息.我遇到的问题是用户HttpPostedFileBase从本地计算机上传文件(使用类),但代码试图匹配服务器上的相同路径,当然文件在服务器上不存在,这就是为什么它会抛出错误这样说
找不到路径的一部分......
如果用户试图C:\Users\User1\Documents\File.txt从本地计算机上传文件,我该如何编写代码以包含文件的整个路径,包括计算机名称和本地驱动器等\\ComputerName\c$\Users\User1\Documnets\File.txt.
我正在使用asp.net mvc-5上载文件,HttpPostedFileBase但我在选择图像后却显示HttpPostedFileBase为空
这是我的代码
<input type="file" title="search image" file-model="profileimage" id="allfilepath" name="file" />
<button type="submit" class="btn btn-primary col-sm-5">
<span class="glyphicon glyphicon-plus"></span>
</button>
Run Code Online (Sandbox Code Playgroud)
和我的控制器
[HttpPost]
public ActionResult insert(HttpPostedFileBase file, quotationcompany quotecomp)
{
var allowedExtensions = new[] {
".Jpg", ".png", ".jpg", "jpeg"
};
if (file == null)
{
ViewBag.message = "Please Select Image";
return View();
}
else {
ViewBag.message = "Image Uploaded Successfully";
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
如果(文件== null)(在此行(
file)在我上载png图片后显示为null)
这段代码有什么问题?
asp.net-mvc ×6
c# ×6
asp.net ×2
file-upload ×2
.net ×1
casting ×1
count ×1
enctype ×1
file ×1
forms ×1
ienumerable ×1
mapped-drive ×1
razor ×1
stream ×1
upload ×1