Fine Uploader仅使用asp.net MVC C#上传null

时间:2018-07-09 09:41:55

标签: c# asp.net-mvc fine-uploader

我有一个使用C#的MVC asp.net FineUploader。我以前在PHP中使用它没有问题,但是在asp.net c#mvc中,它失败了(它触发但总是发布null),有人可以解释为什么吗?我认为这是默认的传输方式,它并不精确,并且实际上并没有使用IFormFile,但是没有FineUploader的任何文档,就无法知道它们依赖于哪种结构进行传输-不管c#控制器是否正确触发,但具有null在里面。

这是<head>标记

 <!-- Fine Uploader Gallery CSS file
====================================================================== -->
<link rel="stylesheet" href="/fine-uploader/fine-uploader-new.css" />
<link rel="stylesheet" href="/fine-uploader/fine-uploader-gallery.css" />


<!-- Fine Uploader JS file
====================================================================== -->
<script src="/fine-uploader/fine-uploader.core.js"></script>
<script src="/fine-uploader/fine-uploader.js"></script>    

<!-- Fine Uploader Gallery template
====================================================================== -->
<script type="text/template" id="qq-template-gallery">
    <div class="qq-uploader-selector qq-uploader qq-gallery" qq-drop-area-text="Drop files here">
        <div class="qq-total-progress-bar-container-selector qq-total-progress-bar-container">
            <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-total-progress-bar-selector qq-progress-bar qq-total-progress-bar"></div>
        </div>
        <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
            <span class="qq-upload-drop-area-text-selector"></span>
        </div>
        <div class="qq-upload-button-selector qq-upload-button">
            <div>Upload a file</div>
        </div>
        <span class="qq-drop-processing-selector qq-drop-processing">
            <span>Processing dropped files...</span>
            <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
        </span>
        <ul class="qq-upload-list-selector qq-upload-list" role="region" aria-live="polite" aria-relevant="additions removals">
            <li>
                <span role="status" class="qq-upload-status-text-selector qq-upload-status-text"></span>
                <div class="qq-progress-bar-container-selector qq-progress-bar-container">
                    <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-progress-bar-selector qq-progress-bar"></div>
                </div>
                <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
                <div class="qq-thumbnail-wrapper">
                    <img class="qq-thumbnail-selector" qq-max-size="120" qq-server-scale>
                </div>
                <button type="button" class="qq-upload-cancel-selector qq-upload-cancel">X</button>
                <button type="button" class="qq-upload-retry-selector qq-upload-retry">
                    <span class="qq-btn qq-retry-icon" aria-label="Retry"></span>
                    Retry
                </button>

                <div class="qq-file-info">
                    <div class="qq-file-name">
                        <span class="qq-upload-file-selector qq-upload-file"></span>
                        <span class="qq-edit-filename-icon-selector qq-edit-filename-icon" aria-label="Edit filename"></span>
                    </div>
                    <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
                    <span class="qq-upload-size-selector qq-upload-size"></span>
                    <button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete">
                        <span class="qq-btn qq-delete-icon" aria-label="Delete"></span>
                    </button>
                    <button type="button" class="qq-btn qq-upload-pause-selector qq-upload-pause">
                        <span class="qq-btn qq-pause-icon" aria-label="Pause"></span>
                    </button>
                    <button type="button" class="qq-btn qq-upload-continue-selector qq-upload-continue">
                        <span class="qq-btn qq-continue-icon" aria-label="Continue"></span>
                    </button>
                </div>
            </li>
        </ul>

        <dialog class="qq-alert-dialog-selector">
            <div class="qq-dialog-message-selector"></div>
            <div class="qq-dialog-buttons">
                <button type="button" class="qq-cancel-button-selector">Close</button>
            </div>
        </dialog>

        <dialog class="qq-confirm-dialog-selector">
            <div class="qq-dialog-message-selector"></div>
            <div class="qq-dialog-buttons">
                <button type="button" class="qq-cancel-button-selector">No</button>
                <button type="button" class="qq-ok-button-selector">Yes</button>
            </div>
        </dialog>

        <dialog class="qq-prompt-dialog-selector">
            <div class="qq-dialog-message-selector"></div>
            <input type="text">
            <div class="qq-dialog-buttons">
                <button type="button" class="qq-cancel-button-selector">Cancel</button>
                <button type="button" class="qq-ok-button-selector">Ok</button>
            </div>
        </dialog>
    </div>
</script>

这是<body>标记

        <!-- Fine Uploader DOM Element
            ====================================================================== -->
        <div id="fine-uploader-gallery"></div>

        <!-- Your code to create an instance of Fine Uploader and bind to the DOM/template
               ====================================================================== -->
        <script>
            var galleryUploader = new qq.FineUploader({
                element: document.getElementById("fine-uploader-gallery"),
                template: 'qq-template-gallery',
                request: {
                    endpoint: '@Url.Action("UploadFile", "Home")'
                },
                thumbnails: {
                    placeholders: {
                        waitingPath: '/fine-uploader/waiting-generic.png',
                        notAvailablePath: '/fine-uploader/not_available-generic.png'
                    }
                },
                validation: {
                    allowedExtensions: ['jpeg', 'jpg', 'gif', 'png']
                }
            });
        </script>

这里是HomeController.cs控制器

    [HttpPost]
    public IActionResult UploadFile(IFormFile file)
    {
        if (file != null)
        {
            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                string contentAsString = reader.ReadToEnd();
                byte[] contentAsByteArray = GetBytes(contentAsString);
                return File(contentAsByteArray, file.ContentType);
            }
        }
        return null;
    }

我知道它会触发服务器,因为当我上传三个项目时,控制器会触发3次。但全部为空。

FineUploader failing

顺便说一句,为了获得奖励积分,即使文件路径正确,我也总是会遇到以下控制台错误

enter image description here

1 个答案:

答案 0 :(得分:0)

  1. 默认情况下,Fine Uploader将在多部分编码的POST请求的正文中发送文件。您可以使用Request对象代替方法参数。
  2. 服务器应为所有请求返回有效的JSON响应。

    [HttpPost]
    public ActionResult UploadFiles()
    {
        string path = Server.MapPath("~/Uploads/");
    
        var fileExists = Request.Files.Count > 0;
    
        if (fileExists)
        {
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
    
            try
            {
                HttpPostedFileBase fileBase = Request.Files[0];
                if (fileBase != null)
                {
                    string fileName = Path.GetFileName(fileBase.FileName);
                    fileBase.SaveAs(path + fileName);
                }
    
                return Json(new { success = "true"});
            }
            catch (Exception ex)
            {
                return Json(new { success = "false", error = ex.Message });
            }
        }
        return Json(new { success = "false"});
    }