【发布时间】:2014-10-21 05:05:08
【问题描述】:
我正在使用summernote,我想将图像上传到我的网络服务器。下面是我正在使用的代码
默认.aspx
<script type="text/javascript">
$(document).ready(function () {
$('#summernote').summernote({
onImageUpload: function (files, editor, $editable) {
alert('image?');
var formData = new FormData();
formData.append("file", files[0]);
$.ajax({
url: "Default.aspx/UploadImage",
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
success: function (imageUrl) {
alert(imageUrl);
if (!imageUrl) {
// handle error
return;
}
editor.insertImage($editable, imageUrl);
},
error: function () {
alert('error');
// handle error
}
});
console.log('image upload:', files, editor, $editable);
}
});
});
</script>
默认.asp.cs
[System.Web.Services.WebMethod]
public static string UploadImage(HttpPostedFileBase file)
{
//Saving to Server
return imageUrl;
}
但它没有调用服务器端函数。此外,alert(imageUrl) 正在给我完整的页面 html。
我哪里错了?
使用网络信息更新问题(谷歌浏览器)
编辑 2
建议后更新
代码:
url: "About.aspx/UploadImage",
data: "multipart/form-data",
type: 'POST',
cache: false,
contentType: "application/json",
错误:
注意:我已将页面从 Default.aspx 更改为 about.aspx(但代码相同)
【问题讨论】:
-
检查 URL - “Default.aspx/UploadImage”,这可能是问题所在。为了进一步调试打开 chrome 网络选项卡 (F12) 并检查状态代码和 AJAX 调用的响应。
-
附带问题,您持有该方法的类是否具有
[ScriptService]属性? -
@abhi 我已经上传了我的问题
-
POST 调用的响应是什么? 400 bad request 也是同一个呼叫的一部分吗?
-
@Holybreath 在 default.aspx.cs 页面中 public partial class _Default : Page