【发布时间】:2015-01-22 11:09:56
【问题描述】:
我有一个图片上传代码。我为 uploading images without postback 使用了 AjaxUpload.js 插件。问题是当我传递硬编码值(无论是作为查询参数还是通过data 助手)时,我能够在我的处理程序中检索它。但是当我传递一些像 $('#some_id').val() 这样的通用值时,我总是在我的处理程序中收到空字符串。
脚本
var upload='';
upload = new AjaxUpload($('#fileOpenDialog'),
{
action: RootPath + 'GraphicResourceHandlers/FileHandler.ashx',
data: { OldGraphicName: $('#hidGraphicName').val() },
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
autoSubmit: false,
onChange: function (file, extension) {
//some code
},
onSubmit: function (file, response) {
alert("Success.");
},
onError: function () {
alert("Error in upload.");
},
onComplete: function (file, response) {
//some code
}
});
//upload image
$('#btnUpload').click(function () {
//This will call the AjaxUpload .
upload.submit();
//Hides the modal that asks for uploading image
$('#browseFile').modal('hide');
});
FileHandler.ashx
public void ProcessRequest(HttpContext context)
{
try
{
context.Response.ContentType = " text/html";
//This line is returning an empty string
context.Request["OldGraphicName"]
//I also did this while passing as a query paramter but no luck
context.Request.QueryString["OldGraphicName"]
string graphicPath = Config.GraphicsPath;
string locationPath = HttpContext.Current.Request.PhysicalApplicationPath + graphicPath;
string fileName = context.Request.Files[0].FileName;
string newFileName = Guid.NewGuid() + Path.GetExtension(fileName);
context.Request.Files[0].SaveAs(locationPath + newFileName);
context.Response.Write(newFileName);
}
}
有人可以调查一下这个问题吗?我真的很努力地找出我的问题所在。
谢谢!
【问题讨论】:
-
我可以通过这个
script在服务器上上传具有唯一名称的图像。现在我需要用旧文件重命名新上传的文件。为此,我将旧图像名称保存在隐藏字段中,并将其作为参数通过 AjaxUpload 传递给处理程序。 -
@Pete ??我在添加我的评论时忘记引用你了
标签: javascript c# jquery asp.net-ajax ajax-upload