如何在ASP.NET MVC中执行图像的Ajax / JQuery上载?
发布时间:2020-05-30 01:02:42 所属栏目:asp.Net 来源:互联网
导读:我有一个用ASP.NET MVC编写的站点.我有一个页面,用户可以在其中创建一篇小文章.在本文中,他们可以选择一个图像.我有一个页面,他们可以上传他们的图像,在创建文章页面上,只需列出它们.但是很多人抱怨他们在发现他们没有上传他们需要的图像之前写了整篇文章.我
|
我有一个用ASP.NET MVC编写的站点.我有一个页面,用户可以在其中创建一篇小文章.在本文中,他们可以选择一个图像.我有一个页面,他们可以上传他们的图像,在创建文章页面上,只需列出它们.但是很多人抱怨他们在发现他们没有上传他们需要的图像之前写了整篇文章.我想要的是让用户能够从创建文章页面上传图像,然后重新加载我可能的图像下拉列表进行选择. 我想在页面上制作一个单独的多部分表单,并让用户在那里选择一个文件(基本上使用我现有的上传功能).但是我如何提交异步?如何重新渲染我的图像列表异步? 如何使用jquery / ajax上传此图像,然后重新填充我的下拉列表? /干杯 解决方法我已经使用过这个 jQuery pluging了几次.我将上传按钮放在使用PopupImageUploader元素的 jQuery UI modal dialog中. <div id="PopupImageUploader" title="Upload Image">
<div id="uploaderFile"></div>
</div>
我的javascript在元素upladerFile上构建上传器 function CreateImageUploader() {
var uploader = new qq.FileUploader({
element: $('#uploaderFile')[0],template: '<div class="qq-uploader">' +
'<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' +
'<div class="qq-upload-button ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default">Seleziona il Listino Excel</div>' +
'<ul class="qq-upload-list"></ul>' +
'</div>',hoverClass: 'ui-state-hover',focusClass: 'ui-state-focus',action: 'Home/UploadImage',allowedExtensions: ['jpg','gif'],params: { },onSubmit: function(file,ext) {
},onComplete: function(id,fileName,responseJSON) {
$("#PopupImageUploader").dialog('close');
}
}
});
}
您可以使用onComplete事件检查上传结果,并最终更新您的下拉列表. [HttpPost()]
public System.String UploadImage(string id)
{
bool IsIE = false;
string sFileName = "";
var TempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"_TEMP");
if ((Request.Files == null) || (Request.Files.Count == 0))
{
if (string.IsNullOrEmpty(Request.Params["qqfile"]))
{
return ("{success:false,error:'request file is empty'}");
}
else
{
sFileName = Request.Params["qqfile"].ToString();
}
}
else
{
sFileName = Request.Files[0].FileName;
IsIE = true;
}
if (string.IsNullOrEmpty(sFileName))
{
return ("{success:false,error:'request file is empty'}");
}
string DocumentName = id + Path.GetExtension(sFileName);
if (IsIE)
{
try
{
Request.Files[0].SaveAs(Path.Combine(TempFolder,DocumentName));
}
catch (Exception ex)
{
return ("{success:false,error:'" + ex.Message + "'}");
}
}
else
{
try
{
if ((Request.InputStream != null) && (Request.InputStream.CanRead) && (Request.InputStream.Length > 0))
{
using (FileStream fileStream = new FileStream(Path.Combine(TempFolder,DocumentName),FileMode.Create))
{
byte[] FileBytes = new byte[Convert.ToInt32(Request.InputStream.Length) + 1];
Int32 bytesRead = 0;
bytesRead = Request.InputStream.Read(FileBytes,FileBytes.Length);
fileStream.Write(FileBytes,bytesRead);
fileStream.Flush();
fileStream.Close();
}
}
}
catch (Exception ex)
{
return ("{success:false,error:'" + ex.Message + "'}");
}
}
var newFileName = "new assigned filename";
return ("{success:true,newfilename: '" + newFileName + "'}");
}
IE有不同的行为,所以我有两个不同的过程来读取文件. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-ajax – ASP.Net AJAX UpdatePanel无法触发Selecte
- asp.net-mvc – MVC – Asp.Net Identity. HOWTO在用户登录
- asp.net-mvc – ASP.NET MVC Web API缓存控制头部没有发送响
- asp.net – IIS 7如何使用数据库中的用户帐户映射客户端证书
- asp.net – 分类失败.返回未最终内容
- asp.net – 调用RenderSection两次?
- asp.net-mvc-3 – 从MVC 3中使用Razor View引擎的局部视图渲
- asp.net – 替代SSRS前端
- 为什么ASP.NET回发时请求cookie属性为null或不正确?
- asp.net-mvc – 无法让defaultRedirect工作
