jquery fileupload插件上传文件的问题

ttddlplp 2016-11-30 03:24:35
在做一个文件上传系统,使用了jquery fileupload插件 环境是asp.net

upload.aspx这边

<form id="fileupload" method="POST" enctype="multipart/form-data">
<div class="row-fluid fileupload-buttonbar">
<div class="span7">
<span class="btn green fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add files...</span>
<input type="file" name="files[]" multiple>
</span>
<button type="submit" class="btn blue start">
<i class="icon-upload icon-white"></i>
<span>Start upload</span>
</button>
</div>
<div class="span5 fileupload-progress fade">
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="bar" style="width:0%;"></div>
</div>
<div class="progress-extended"> </div>
</div>
</div>
<div class="fileupload-loading"></div>
<br>
<table role="presentation" class="table table-striped">
<tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody>
</table>
</form>

<div class="span12">
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td class="preview"><span class="fade"></span></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
{% if (file.error) { %}
<td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td>
{% } else if (o.files.valid && !i) { %}
<td>
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>
</td>
<td class="start">{% if (!o.options.autoUpload) { %}
<button class="btn">
<i class="icon-upload icon-white"></i>
<span>上传</span>
</button>
{% } %}</td>
{% } else { %}
<td colspan="2"></td>
{% } %}
<td class="cancel">{% if (!i) { %}
<button class="btn red">
<i class="icon-ban-circle icon-white"></i>
<span>取消</span>
</button>
{% } %}</td>
</tr>
{% } %}
</script>
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td>
{% } else { %}<td class="preview">
{% if (file.thumbnail_url) { %}
<a class="fancybox-button" data-rel="fancybox-button" href="{%=file.url%}" title="{%=file.name%}">
<img src="media/image/{%=file.thumbnail_url%}">
</a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
</td>
</tr>
{% } %}
</script>
</div>


upload.aspx这边的js

$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: 'uploadfile.ashx?Action=upload',
});

// Load existing files:
// Demo settings:
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url'),
context: $('#fileupload')[0],
maxFileSize: 5000000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
process: [{
action: 'load',
fileTypes: /^image\/(gif|jpeg|png)$/,
maxFileSize: 20000000 // 20MB
}, {
action: 'resize',
maxWidth: 1440,
maxHeight: 900
}, {
action: 'save'
}
],
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, null, {
result: result
});
})



uploadfile.ashx后台

public class uploadfile1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ContentType = "text/plain";
System.Collections.Specialized.NameValueCollection forms = context.Request.Form;
HttpFileCollection files = context.Request.Files;
int num = files.Count;
string key = forms.Get("dKeyword");
if (num > 0)
{
for (int i = 0; i < num; i++)
{
HttpPostedFile file = files[i];
int fileSizeInBytes = file.ContentLength;
string filename = Path.GetFileNameWithoutExtension(file.FileName);
}
}
}

public bool IsReusable
{
get
{
return false;
}
}
}



ashx这边已经可以 获取文件名 进行上传

但是如果ashx 不加context.Response.Write(“finished”); 插件这边就显示Empty file upload result
加了context.Response.Write(“finished”); 插件这边就显示SyntaxError: 无效字符

那么问题就是 ashx如何返回信息给插件 上传成功或者存在重名或者上传失败信息并在template-download里面显示呢?
...全文
592 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2016-12-01
  • 打赏
  • 举报
回复
自己去看插件api,需要服务器端返回什么样的数据

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧