asp.net-mvc – 在mvc中拖放文件
发布时间:2020-05-25 05:23:38 所属栏目:asp.Net 来源:互联网
导读:我想使用拖放来上传文件.我编写了如下代码,但每次尝试上传文件时,都显示上传失败.谁能告诉我哪里错了?我想从外部源拖动项目并将其上传到我的文件夹但我无法做到. 对于控制器: – public ActionResult File(){ return View();}/// summary/// The max file s
|
我想使用拖放来上传文件.我编写了如下代码,但每次尝试上传文件时,都显示上传失败.谁能告诉我哪里错了?我想从外部源拖动项目并将其上传到我的文件夹但我无法做到. 对于控制器: – public ActionResult File()
{
return View();
}
/// <summary>
/// The max file size in bytes
/// </summary>
protected int maxRequestLength
{
get
{
HttpRuntimeSection section =
ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (section != null)
return section.MaxRequestLength * 1024; // Default Value
else
return 4096 * 1024; // Default Value
}
}
/// <summary>
/// Checks if a file is sent to the server
/// and saves it to the Uploads folder.
/// </summary>
[HttpPost]
private void handleFileUpload()
{
if (!string.IsNullOrEmpty(Request.Headers["X-File-Name"]))
{
string path = Server.MapPath(string.Format("~/Uploads/{0}",Request.Headers["X-File-Name"]));
Stream inputStream = Request.InputStream;
FileStream fileStream = new FileStream(path,FileMode.OpenOrCreate);
inputStream.CopyTo(fileStream);
fileStream.Close();
}
}
为了查看它是: – <!DOCTYPE html>
<html>
<head runat="server">
<title>Drag n' Drop File Upload</title>
<link href="/Style.css" rel="Stylesheet" />
<style>
body
{
font: 12px Arial;
}
#dropZone
{
border-radius: 5px;
border: 2px solid #ccc;
background-color: #eee;
width: 250px;
padding: 50px 0;
text-align: center;
font-size: 18px;
color: #555;
margin: 50px auto;
}
#dropZone.hover
{
border-color: #aaa;
background-color: #ddd;
}
#dropZone.error
{
border-color: #f00;
background-color: #faa;
}
</style>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
var dropZone;
// Initializes the dropZone
$(document).ready(function () {
dropZone = $('#dropZone');
dropZone.removeClass('error');
// Check if window.FileReader exists to make
// sure the browser supports file uploads
if (typeof(window.FileReader) == 'undefined') {
dropZone.text('Browser Not Supported!');
dropZone.addClass('error');
return;
}
// Add a nice drag effect
dropZone[0].ondragover = function () {
dropZone.addClass('hover');
return false;
};
// Remove the drag effect when stopping our drag
dropZone[0].ondragend = function () {
dropZone.removeClass('hover');
return false;
};
// The drop event handles the file sending
dropZone[0].ondrop = function(event) {
// Stop the browser from opening the file in the window
event.preventDefault();
dropZone.removeClass('hover');
// Get the file and the file reader
var file = event.dataTransfer.files[0];
@* if(file.size > @maxRequestLength {
dropZone.text('File Too Large!');
dropZone.addClass('error');
return false;*@
// // Validate file size
// if(file.size > <%=maxRequestLength%>) {
// dropZone.text('File Too Large!');
// dropZone.addClass('error');
// return false;
/ |
相关内容
- asp.net – MVC5(VS2012)Identity CreateIdentityAsync –
- 我可以在ASP.NET中获取浏览器时区,还是依靠JS操作来检索信息
- asp.net-mvc – 用于控件名称的参数化前缀的局部视图
- asp.net – 如何限制指定文件类型的下载
- asp.net-mvc – 如何使用具有可空类型的强类型HTML帮助程序
- asp.net – 如何在IIS Express下启用区分大小写?
- 在ASP.NET应用程序中托管的WCF服务中使用Autofac作为DI
- 为什么我需要在ASP.NET控件上调用处理?
- 在asp.net控件的style属性中使用DataBinder.Eval()
- asp.net – Azure网站上脚本/样式的长时间等待(TTFB)时间
推荐文章
站长推荐
- asp.net-mvc-3 – System.Web.Mvc.HtmlHelper’不
- 在Azure网络应用程序中显示ASP.NET 5错误页面
- 在asp.net mvc 3中实现FilterAttribute,IActionF
- asp.net-mvc – MVC网站转发到/ Account / Login
- asp.net-mvc – 具有接受routeValues和htmlAttri
- asp.net-mvc – MVC SelectList不能正常工作
- asp.net-mvc – 帖子上的Mvc模型ID 0
- asp.net-mvc – 我可以免费在ASP.NET MVC上开发吗
- asp.net-mvc-3 – ASP.Net Mvc 3 Url.Action方法
- asp.net – 调用en外部javascript文件的函数
热点阅读
