从 jQuery 文件上传插件的控制器操作返回什么?

发布于 2025-01-04 04:44:20 字数 817 浏览 1 评论 0原文

我正在使用 Blueimp 的 jQuery 文件上传 5.6 版提供的演示页面。我可以让演示在我的 ASP.NET MVC 项目中运行,直到可以从页面上传文件。

但是,即使文件成功上传,UI 也会报告错误。我 100% 确定此错误是因为我没有从控制器操作中返回正确的信息。

这是我现有的操作:

    [HttpPost]
    public virtual JsonResult ImageUpload(FormCollection formdata)
    {
        var imagePath = Setting.Load(SettingKey.BlogImagesBasePath).Value;
        for(var i = 0; i < Request.Files.Count; i++)
        {
            var saveFileName = string.Format("{0}\\{1}", imagePath, Request.Files[i].FileName);
            Log.DebugFormat("Attempting to save file: {0}", saveFileName);
            Request.Files[i].SaveAs(saveFileName);
        }

        return Json(new {});
    }

我不知道结果的内容应该是什么。我尝试对 php 示例进行排序,但由于完全不熟悉 php,我所能做的最好的事情就是可能涉及文件名、大小和类型。

有人有工作 MVC 示例的链接或提供我将正确数据返回到插件所需的信息吗?

I'm using the demo page provided with the jQuery File Upload version 5.6 from Blueimp. I can get the demo working in my ASP.NET MVC project to the point where a file can be uploaded from the page.

However, even though the file successfully gets uploaded, the UI reports an error. I'm 100% certain this error is because I'm not returning the proper information from my controller action.

Here is my existing action:

    [HttpPost]
    public virtual JsonResult ImageUpload(FormCollection formdata)
    {
        var imagePath = Setting.Load(SettingKey.BlogImagesBasePath).Value;
        for(var i = 0; i < Request.Files.Count; i++)
        {
            var saveFileName = string.Format("{0}\\{1}", imagePath, Request.Files[i].FileName);
            Log.DebugFormat("Attempting to save file: {0}", saveFileName);
            Request.Files[i].SaveAs(saveFileName);
        }

        return Json(new {});
    }

I don't know what the content of the result should be. I tried sorting through the php example but, not being at all familiar with php, the best I could make of it is that the filename, size, and type might be involved.

Does somebody have a link to a working MVC sample or provide the information I need to return the correct data to the plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-01-11 04:44:20

帆船柔道

我认为这个问题100%满足您的需求:

jQuery 文件上传插件要求我下载文件,出了什么问题?

这里基本上是它演示的内容:

类:

public class ViewDataUploadFilesResult
{
    public string Name { get; set; }
    public int Length { get; set; }
    public string Type { get; set; }
}

操作:

[HttpPost]
public JsonResult UploadFiles()
{
    var r = new List<ViewDataUploadFilesResult>();
    Core.Settings settings = new Core.Settings();
    foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength == 0)
            continue;
        string savedFileName = Path.Combine(settings.StorageLocation + "\\Files\\", Path.GetFileName(hpf.FileName));
        hpf.SaveAs(savedFileName);

        r.Add(new ViewDataUploadFilesResult()
        {
            Name = hpf.FileName,
            Length = hpf.ContentLength,
            Type = hpf.ContentType
        });
    }
    return Json(r);
}

所以,基本上,您只需要返回ViewDataUploadFilesResult 集合的 jsonresult。

希望有帮助。

Sailing Judo

I think this question addresses your needs 100%:

jQuery File Upload plugin asks me to download the file, what is wrong?

here's basically what it demos:

the class:

public class ViewDataUploadFilesResult
{
    public string Name { get; set; }
    public int Length { get; set; }
    public string Type { get; set; }
}

the action:

[HttpPost]
public JsonResult UploadFiles()
{
    var r = new List<ViewDataUploadFilesResult>();
    Core.Settings settings = new Core.Settings();
    foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength == 0)
            continue;
        string savedFileName = Path.Combine(settings.StorageLocation + "\\Files\\", Path.GetFileName(hpf.FileName));
        hpf.SaveAs(savedFileName);

        r.Add(new ViewDataUploadFilesResult()
        {
            Name = hpf.FileName,
            Length = hpf.ContentLength,
            Type = hpf.ContentType
        });
    }
    return Json(r);
}

so, basically, you'd just need to return the jsonresult of the ViewDataUploadFilesResult collection.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文