gpt4 book ai didi

c# - MVC-4 文件上传成功消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:19:07 25 4
gpt4 key购买 nike

我在上传文件后显示成功消息时遇到了一些问题。

我首先尝试使用 ViewBag.Message ,它运行良好并在文件上传后显示成功消息,这正是我想要的。但是后来我找不到方法,几秒钟后将该消息更改回:“选择要上传的文件!” ,以便用户了解他现在可以上传新文件。

我尝试实现一个 javascript 功能来处理成功消息。这样做的问题是在文件上传完成之前会显示成功消息,这是不好的,如果它是一个非常小的文件,消息只会显示一毫秒。

您对我如何微调它有什么建议吗?我不确定我是否应该尝试使用 javascript 或 viewbag 或其他东西进一步工作?

我正在寻找的是成功上传后显示约 5 秒的成功消息,然后再次变回“选择要上传的文件消息”。

https://github.com/xoxotw/mvc_fileUploader

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace Mvc_fileUploader.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
//ViewBag.Message = "Choose a file to upload !";
return View("FileUpload");
}

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase fileToUpload)
{

if (ModelState.IsValid)
{
if (fileToUpload != null && fileToUpload.ContentLength > (1024 * 1024 * 2000)) // 1MB limit
{
ModelState.AddModelError("fileToUpload", "Your file is to large. Maximum size allowed is 1MB !");
}

else
{
string fileName = Path.GetFileName(fileToUpload.FileName);
string directory = Server.MapPath("~/fileUploads/");

if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

string path = Path.Combine(directory, fileName);
fileToUpload.SaveAs(path);

ModelState.Clear();
//ViewBag.Message = "File uploaded successfully !";

}
}

return View("FileUpload");

}



public ActionResult About()
{
ViewBag.Message = "Your app description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}

文件上传 View :

@{
ViewBag.Title = "FileUpload";
}

<h2>FileUpload</h2>

<h3>Upload a File:</h3>


@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.ValidationSummary();
<input type="file" name="fileToUpload" /><br />
<input type="submit" onclick="successMessage()" name="Submit" value="upload" />
//@ViewBag.Message
<span id="sM">Choose a file to upload !</span>
}


<script>
function successMessage()
{
x = document.getElementById("sM");
x.innerHTML = "File upload successful !";
}
</script>

最佳答案

几件事,

首先,您需要一个模型来指示上传成功,我们可以在您的实例中使用 bool 来指示它。

将此添加到 View 的顶部:

@model bool

然后你可以这样做(保持你的观点不变):

@{
ViewBag.Title = "FileUpload";
}

<h2>FileUpload</h2>

<h3>Upload a File:</h3>

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.ValidationSummary();
<input type="file" name="fileToUpload" /><br />
<input type="submit" onclick="successMessage()" name="Submit" value="upload" />

<span id="sM">Choose a file to upload !</span>
}

我们可以根据模型值在JS中操作sM

<script>

@if(Model)
{
var x = document.getElementById("sM");
x.innerHTML = "File upload successful !";
setTimeout("revertSuccessMessage()", 5000);
}

function revertSuccessMessage()
{
var x = document.getElementById("sM");
x.innerHTML = "Choose a file to upload !";
}
</script>

然后在操作方法的 else 语句中,确保成功时返回 true,否则返回 false。像这样

else
{
string fileName = Path.GetFileName(fileToUpload.FileName);
string directory = Server.MapPath("~/fileUploads/");

if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

string path = Path.Combine(directory, fileName);
fileToUpload.SaveAs(path);

ModelState.Clear();

return View("FileUpload", true);
}

return View("FileUpload", false);

关于c# - MVC-4 文件上传成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16666122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com