FineUploader - MVC 应用程序中的 500 服务器错误
FineUploader - 500 Server Error in MVC app
刚开始学习FineUploader。我相信我已经正确设置了一切。但是,当我尝试上传文件时,我的 JavaScript 代码中出现 500 服务器错误。我的控制器方法(如下所示)永远不会被调用。所以它发生在将控制权传递给方法之前。
[HttpPost]
public FineUploaderResult UploadFile(FineUpload upload, string extraParam1, int extraParam2)
{
// Asp.net MVC will set extraParam1 and extraParam2 from the params object passed by Fine-Uploader
string dir = @"c:\upload\path";
var filePath = Path.Combine(dir, upload.Filename);
try
{
upload.SaveAs(filePath);
}
catch (Exception ex)
{
return new FineUploaderResult(false, error: ex.Message);
}
// the anonymous object in the result below will be convert to json and set back to the browser
return new FineUploaderResult(true, new { extraInformation = 12345 });
}
我的JavaScript代码如下:
$(document).ready(function () {
$("#fine-uploader").fineUploader({
request: {
endpoint: 'UploadFile'
}
});
});
我的ModelBinderclass正在执行,如下:
public class ModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.RequestContext.HttpContext.Request;
var formUpload = request.Files.Count > 0;
// find filename
var xFileName = request.Headers["X-File-Name"];
var qqFile = request["qqfile"];
var formFilename = formUpload ? request.Files[0].FileName : null;
var upload = new FineUpload
{
Filename = xFileName ?? qqFile ?? formFilename,
InputStream = formUpload ? request.Files[0].InputStream : request.InputStream
};
return upload;
}
}
如果您的预期端点未命中并且您的服务器返回 500 响应请求,则问题出在您的服务器或 Fine Uploader 的客户端配置上。您的端点选项 "UploadFile" 似乎很可疑。我希望输入路径。
对于遇到此 post 的任何其他人,我的问题在于 UploadController.cs 中的以下代码行:
public FineUploaderResult UploadFile(FineUpload upload, string extraParam1, int extraParam2)
BindModel 在 FineUpload.cs 中被调用,但之后我收到 500 错误,调试中没有方法被触发。
代码是我的cshtml页面如下:
<div id="fine-uploader-gallery"></div>
<script>
$("#fine-uploader-gallery").fineUploader({
template: 'qq-template-gallery',
request: {
endpoint: '@Url.Action("UploadFile", "Upload")'
},
thumbnails: {
placeholders: {
waitingPath: '/source/placeholders/waiting-generic.png',
notAvailablePath: '/source/placeholders/not_available-generic.png'
}
},
validation: {
allowedExtensions: ['jpeg', 'jpg', 'png'],
itemLimit: 3,
sizeLimit: 102400 // 50 kB = 50 * 1024 bytes
},
autoUpload: false,
debug: true
});
$("#trigger-upload").click(function () {
$("#fine-uploader-gallery").fineUploader("uploadStoredFiles");
});
</script>
这可能是我对这个优秀产品缺乏了解,但我唯一能让它工作的方法是将上面的问题行更改为
public FineUploaderResult UploadFile(FineUpload upload)
这样调用的签名与控制器中的方法相匹配。
刚开始学习FineUploader。我相信我已经正确设置了一切。但是,当我尝试上传文件时,我的 JavaScript 代码中出现 500 服务器错误。我的控制器方法(如下所示)永远不会被调用。所以它发生在将控制权传递给方法之前。
[HttpPost]
public FineUploaderResult UploadFile(FineUpload upload, string extraParam1, int extraParam2)
{
// Asp.net MVC will set extraParam1 and extraParam2 from the params object passed by Fine-Uploader
string dir = @"c:\upload\path";
var filePath = Path.Combine(dir, upload.Filename);
try
{
upload.SaveAs(filePath);
}
catch (Exception ex)
{
return new FineUploaderResult(false, error: ex.Message);
}
// the anonymous object in the result below will be convert to json and set back to the browser
return new FineUploaderResult(true, new { extraInformation = 12345 });
}
我的JavaScript代码如下:
$(document).ready(function () {
$("#fine-uploader").fineUploader({
request: {
endpoint: 'UploadFile'
}
});
});
我的ModelBinderclass正在执行,如下:
public class ModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.RequestContext.HttpContext.Request;
var formUpload = request.Files.Count > 0;
// find filename
var xFileName = request.Headers["X-File-Name"];
var qqFile = request["qqfile"];
var formFilename = formUpload ? request.Files[0].FileName : null;
var upload = new FineUpload
{
Filename = xFileName ?? qqFile ?? formFilename,
InputStream = formUpload ? request.Files[0].InputStream : request.InputStream
};
return upload;
}
}
如果您的预期端点未命中并且您的服务器返回 500 响应请求,则问题出在您的服务器或 Fine Uploader 的客户端配置上。您的端点选项 "UploadFile" 似乎很可疑。我希望输入路径。
对于遇到此 post 的任何其他人,我的问题在于 UploadController.cs 中的以下代码行:
public FineUploaderResult UploadFile(FineUpload upload, string extraParam1, int extraParam2)
BindModel 在 FineUpload.cs 中被调用,但之后我收到 500 错误,调试中没有方法被触发。 代码是我的cshtml页面如下:
<div id="fine-uploader-gallery"></div>
<script>
$("#fine-uploader-gallery").fineUploader({
template: 'qq-template-gallery',
request: {
endpoint: '@Url.Action("UploadFile", "Upload")'
},
thumbnails: {
placeholders: {
waitingPath: '/source/placeholders/waiting-generic.png',
notAvailablePath: '/source/placeholders/not_available-generic.png'
}
},
validation: {
allowedExtensions: ['jpeg', 'jpg', 'png'],
itemLimit: 3,
sizeLimit: 102400 // 50 kB = 50 * 1024 bytes
},
autoUpload: false,
debug: true
});
$("#trigger-upload").click(function () {
$("#fine-uploader-gallery").fineUploader("uploadStoredFiles");
});
</script>
这可能是我对这个优秀产品缺乏了解,但我唯一能让它工作的方法是将上面的问题行更改为
public FineUploaderResult UploadFile(FineUpload upload)
这样调用的签名与控制器中的方法相匹配。