图片上传在 localhost 中工作,但在 IIS 服务器中不工作 [ Asp.net Mvc ]
Image upload working in localhost but not in IIS server [ Asp.net Mvc ]
我正在做一个 MVC 项目,我试图通过 ajax 上传图片;这在本地主机上绝对工作正常,但它在 IIS Web 服务器上不起作用。一旦我按下提交按钮,它就会在控制台上显示错误,说:
"http://blahblahmywebsite.com/UserRegistration/UserPhoto 500(内部服务器错误)"。
我已经为这个问题苦苦挣扎了 2 天,有什么建议吗?
N.B。
-> 我已经试过了:url: '@Url.Action("FunctionName", "Controller")'
-> 在应用程序池中进行操作(其他在线帖子建议)
->我在使用 IIS 8 的 Godaddy 服务器上托管了这个网站。
->我正在为本地服务器使用 .NET 4.5.2
这是我的 jQuery:
$("#fileUserImage").change(function () {
userImage();
});
function userImage() {
var formData = new FormData();
var totalFiles = document.getElementById("fileUserImage").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("fileUserImage").files[i];
formData.append("fileUserImage", file);
}
$.ajax({
type: "POST",
url: '/UserRegistration/UserPhoto',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (image) {
$("#imgUserImage").attr('src', '../' + image);
alert("Entered into success block");
},
error: function (error) {
alert("Entered into error block");
}
});
}
这是我的 Html:
<div class="form-group">
<label class="col-md-5 control-label">Photo</label>
<div class="col-md-7">
<input type="file" id="fileUserImage" name="fileUserImage">
</div>
</div>
<div class="form-group">
<div class="img-wrap col-md-offset-5">
<img class="example-image" id="imgUserImage" width="220" style="padding-left:17px;" />
</div>
</div>
这是我的控制器:
[HttpPost]
public JsonResult UserPhoto()
{
var userPic = "";
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/User/"), DateTime.Now.Day + "" + DateTime.Now.Month + "" + DateTime.Now.Year + fileName);
file.SaveAs(path);
userPic = "Images/User/" + DateTime.Now.Day + "" + DateTime.Now.Month + "" + DateTime.Now.Year + fileName;
}
else
{
userPic = "";
}
}
return Json(userPic, JsonRequestBehavior.AllowGet);
}
检查您的目标文件夹 --> 右键单击该文件夹 --> 属性 --> 安全选项卡 --> 编辑按钮 --> 允许安全。请参见下图。
我在远程服务器上托管了我的网站,但遇到了同样的问题。后来我发现应用程序用户没有图像文件夹的add/edit权限。
我正在做一个 MVC 项目,我试图通过 ajax 上传图片;这在本地主机上绝对工作正常,但它在 IIS Web 服务器上不起作用。一旦我按下提交按钮,它就会在控制台上显示错误,说:
"http://blahblahmywebsite.com/UserRegistration/UserPhoto 500(内部服务器错误)"。
我已经为这个问题苦苦挣扎了 2 天,有什么建议吗?
N.B。
-> 我已经试过了:url: '@Url.Action("FunctionName", "Controller")'
-> 在应用程序池中进行操作(其他在线帖子建议)
->我在使用 IIS 8 的 Godaddy 服务器上托管了这个网站。
->我正在为本地服务器使用 .NET 4.5.2
这是我的 jQuery:
$("#fileUserImage").change(function () {
userImage();
});
function userImage() {
var formData = new FormData();
var totalFiles = document.getElementById("fileUserImage").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("fileUserImage").files[i];
formData.append("fileUserImage", file);
}
$.ajax({
type: "POST",
url: '/UserRegistration/UserPhoto',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (image) {
$("#imgUserImage").attr('src', '../' + image);
alert("Entered into success block");
},
error: function (error) {
alert("Entered into error block");
}
});
}
这是我的 Html:
<div class="form-group">
<label class="col-md-5 control-label">Photo</label>
<div class="col-md-7">
<input type="file" id="fileUserImage" name="fileUserImage">
</div>
</div>
<div class="form-group">
<div class="img-wrap col-md-offset-5">
<img class="example-image" id="imgUserImage" width="220" style="padding-left:17px;" />
</div>
</div>
这是我的控制器:
[HttpPost]
public JsonResult UserPhoto()
{
var userPic = "";
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/User/"), DateTime.Now.Day + "" + DateTime.Now.Month + "" + DateTime.Now.Year + fileName);
file.SaveAs(path);
userPic = "Images/User/" + DateTime.Now.Day + "" + DateTime.Now.Month + "" + DateTime.Now.Year + fileName;
}
else
{
userPic = "";
}
}
return Json(userPic, JsonRequestBehavior.AllowGet);
}
检查您的目标文件夹 --> 右键单击该文件夹 --> 属性 --> 安全选项卡 --> 编辑按钮 --> 允许安全。请参见下图。
我在远程服务器上托管了我的网站,但遇到了同样的问题。后来我发现应用程序用户没有图像文件夹的add/edit权限。