gpt4 book ai didi

asp.net-mvc - 需要帮助使用 ASP.NET MVC2 调试基于 XHR 的 Ajax 图像上传

转载 作者:行者123 更新时间:2023-12-02 03:00:23 25 4
gpt4 key购买 nike

我正在尝试使用从 http://valums.com/ajax-upload/ 找到的脚本

我的 Controller 如下

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using MHNHub.Areas.ViewModels;
using MHNHub.Models;
using MHNHub.ViewModels;

namespace MHNHub.Areas.Admin.Controllers
{
[Authorize(Roles = "Administrator")]
public class ImageController : Controller
{

private MHNHubEntities _entities = new MHNHubEntities();

//
// GET: /Image/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult ImageUploader()
{
var viewModel = new ImageViewModel()
{
Image = new Image()
};

return PartialView(viewModel);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImageUploader(Image image)
{
try
{
_entities.Images.AddObject(image);
_entities.SaveChanges();

return RedirectToAction("Index", "Product");
}
catch (Exception ex)
{
var viewModel = new ImageViewModel()
{
Image = image,
HasError = true,
ErrorMessage = ex.Message
};
return PartialView(viewModel);

}
}

private string _uploadsFolder = HostingEnvironment.MapPath("~/App_Data/Files");

public Guid Upload(HttpPostedFileBase fileBase)
{
var identifier = Guid.NewGuid();
fileBase.SaveAs(GetDiskLocation(identifier));
return identifier;
}

private string GetDiskLocation(Guid identifier)
{
return Path.Combine(_uploadsFolder, identifier.ToString());
}

}

}

我有这样的局部 View

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MHNHub.ViewModels.ImageViewModel>" %>

<script type="text/javascript">
$(function () {
$("#imagedialog").dialog({
bgiframe: true,
height: 170,
width: 430,
modal: true,
autoOpen: false,
resizable: true
})
});

$(document).ready(function createUploader() {
var uploader = new qq.FileUploader({
element: document.getElementById('fileuploader'),
action: '/Image/Upload/',
name: 'name'
});

});

</script>

<div id="imagedialog" title="Upload Image">

<div id="fileuploader">

</div>
<h6>Drag and drop files supported in Firefox and Google Chrome with javascript enabled.</h6>
<noscript>
<form action="/image/upload" enctype="multipart/form-data" method="post">
Select a file: <input type="file" name="photo" id="photo" />

<input type="submit" value="Upload" name="submit"/>
</form>
</noscript>


</div>

<div class="editor-field">
<img src="<%: Model.Image.FileName %>" />
<%: Html.TextBoxFor(model => model.Image.FileName) %>
<%: Html.ValidationMessageFor(model => model.Image.FileName)%>
<a href="#" onclick="jQuery('#imagedialog').dialog('open'); return false">Upload Image</a>
</div>

我在母版页上正确链接了 fileuploader.js 和 fileuploader.css,并且 uploader 显示正确,甚至调用了我的操作,但 HttpPostedFileBase 为 null 并且上传操作引发异常。关于我应该做什么有什么见解吗?

编辑

所以我发现使用 firebug 来发送 XmlHttpRequest。我该如何在上传操作中处理这个问题?

最佳答案

您在 Controller 操作中获得空参数的原因是因为此插件不发送 multipart/form-data向服务器请求。相反,它发送 application/octet-stream内容类型请求 header ,它将文件内容直接写入请求流,并附加参数 ?qqfile到包含文件名的 URL。因此,如果您想在 Controller 上检索此内容,您将需要直接读取流:

[HttpPost]
public ActionResult Upload(string qqfile)
{
using (var reader = new BinaryReader(Request.InputStream))
{
// This will contain the uploaded file data and the qqfile the name
byte[] file = reader.ReadBytes((int)Request.InputStream.Length);
}
return View();
}

如果您选择多个文件,插件只需向服务器发送多个请求,这样就可以工作。

此外,如果您想处理大于 int.MaxValue 的文件您必须从请求流中读取 block 并直接写入输出流,而不是将整个文件加载到内存缓冲区中:

using (var outputStream = File.Create(qqfile))
{
const int chunkSize = 2 * 1024; // 2KB
byte[] buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
}
}

备注:删除createUploader来自您的 document.ready 的函数名称。那里应该是一个匿名函数。您甚至可以将其与 $(function() { ... }); 合并。您已经必须设置模式对话框。

关于asp.net-mvc - 需要帮助使用 ASP.NET MVC2 调试基于 XHR 的 Ajax 图像上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com