1

我在 umbraco 中使用自定义部分来显示用户发送的消息。我希望管理员能够通过将文件上传到消息本身来回答这些消息。这样我就会知道哪个文件属于哪个消息。我遵循了一些指南和建议,最值得注意 的是关于如何从 umbraco 后台上传文件的文件上传在后台自定义部分。

但是,我的代码的问题在于,它似乎并没有发送带有 post 请求的整个文件,只是文件的路径变得类似于:C:\fakepath\file.pdf

我的问题是如何在 post 请求中发送整个文件,以及如何在 API 函数中将文件捕获/转换为 HttpPostedFileBase。如果我可以将文件作为 HttpPostedFileBase 获取,我将知道如何将其上传到 umbraco 的媒体部分。

下面的代码:

编辑.html:

                    <umb-control-group label="File" description="File to upload">
                        <input type="file" class="umb-editor umb-textstring" ng-model="files" ng-change="fileSelected(files)" ng-multiple="false" />
                    </umb-control-group>
                </div>
            </div>
        </div>
    </umb-editor-container>

    <umb-editor-footer>
        <umb-editor-footer-content-right>
            <div class="umb-button ng-scope">           
                <button ng-click="OnSave()" class="btn umb-button__button btn-success umb-button--">
                    <span class="umb-button__content">
                        Save
                    </span>
                </button>
            </div>
        </umb-editor-footer-content-right>
    </umb-editor-footer>

编辑.controller.js:

$scope.fileSelected = function (files) {
        $scope.file = files;
    };


    $scope.OnSave = function () {
            var request = {
                file: $scope.file
            };
            return $http({
                method: 'POST',
                url: "backoffice/Messages/MessagesApi/PostSaveFile",
                headers: { 'Content-Type': undefined },
                transformRequest: function (data) {
                    var formData = new FormData();
                    formData.append("file", data.file);
                    return formData;
                },
                data: request
            }).then(function (response) {
                if (response) {
                    var fileName = response.data;
                    return fileName;
                } else {
                    return false;
                }
            });

    };

MessagesApiController.cs:

public HttpResponseMessage PostSaveFile(HttpPostedFileBase file)
{
    var files = HttpContext.Current.Request;

    if (files != null && files.ContentLength > 0)
    {
        StreamReader reader = new StreamReader(files.InputStream);
        string responseFromServer = reader.ReadToEnd();
    }
    return Request.CreateResponse(HttpStatusCode.OK);
}
4

2 回答 2

2

如果有人在这个问题上绊倒,这就是我设法解决的方法:

编辑.html:

<umb-control-group label="Response File" description="File to upload">
      <input type="file" id="myFile" ng-multiple="false" />
</umb-control-group>

编辑.controller.js:

 if ($element.find('#myFile')[0].files[0] !== undefined) {
                var fd = new FormData();
                fd.append('file', $element.find('#myFile')[0].files[0]);
                $http.post("backoffice/Messages/MessagesApi/PostSaveFile", fd, {
                    transformRequest: angular.identity,
                    headers: { 'Content-Type': undefined }
                }).success(function (response) {
                    console.log(response);
                });
            }

MessagesApiController.cs:

public string PostSaveFile()
{
    var file = HttpContext.Current.Request.Files["file"];
    return file.FileName;
}

变量文件现在是一个 HttpPostedFile,你可以将它用于任何你想要的东西,比如将它保存到媒体部分。

于 2018-12-20T15:07:38.077 回答
1

对于那些在 Umbraco 8 的自定义部分中为文件上传而苦苦挣扎的人,这是我的解决方案:

看法:

<input type="file" class="umb-textstring" ngf-select="" ng-model="files" ng-multiple="false" ngf-change="fileSelected(files)" required />
<umb-button action="vm.clickUploadButton()"
            type="button"
            button-style="info"
            label="Upload">
</umb-button>

控制器:

function Controller($scope, Upload) {
    var vm = this;
    vm.clickUploadButton = clickUploadButton;

    $scope.fileSelected = function (files) {
        $scope.file = files;
    };

    function clickUploadButton() {
        if (!$scope.file)
            return false;

        Upload.upload({
            url: "backoffice/api/Upload/UploadFile",
            fields: {
                "field1": "value1",
                "field2": "value2"
            },
            file: $scope.file
        }).success(function (data, status, headers, config) {
            console.log(status);
            console.log(data);
        }).error(function (evt, status, headers, config) {
            console.log(evt.Message);
        });
    }
}

API控制器:

public class UploadController : UmbracoAuthorizedApiController
{
    [HttpPost]
    public HttpResponseMessage UploadFile()
    {
        var files = HttpContext.Current.Request.Files;
        var field1 = HttpContext.Current.Request.Form["field1"];
        var field2 = HttpContext.Current.Request.Form["field2"];

        return Request.CreateResponse(HttpStatusCode.OK, new { fileCount = files.Count, field1, field2 }) ;
    }
}
于 2020-05-31T17:36:01.470 回答