簡體   English   中英

如何使用角度文件上傳器將文件發布到C#MVC模型?

[英]How to post files to C# MVC model using the angular file uploader?

在我的頁面上,我有一個編寫的簡單聊天客戶端。

消息中包含主題,消息中可以包含文件(結構如下所示: 主題列表。->每個主題都有消息列表。->每個消息都有文檔列表。 )。 我只是不知道如何將這些文件添加到消息中,因為我還不是AngularJS專家(至今!)。

我正在使用Azure服務。

碼:

HTML

<div class="row">
    <div class="col-md-12 text-left">
        <button class="btn btn-default margins" ng-click="backToTopicViewSwitch()">Wróć</button>
        <button class="btn btn-primary margins" ng-click="addNewMessage()">Nowa wiadomość</button>
    </div>
    <form novalidate="" name="form">
        <div class="form-group">
            <textarea class="form-control" placeholder="Treść nowej wiadomości..." type="text" rows="4" name="MsgText" ng-model="dataForNewMessage.MsgText" required=""></textarea>
            <div>
                <label>Dodawanie załączników do wiadomości:</label>
                <input class="form-control" type="file" nv-file-select uploader="uploaderMsg" multiple/>
            </div>
        </div>
    </form>
</div>

addNewMessage()

$scope.addNewMessage = function () {
    $http({ method: 'PUT', url: '/OfferClientData/AddMessageToTopic', data: { response: $scope.data, topicId: $scope.topicMsgsToShow, messageText: $scope.dataForNewMessage.MsgText } }).then(function (d) {
        $scope.data = d.data;
        toaster.pop('success', "Dodawanie wiadomości", "Pomyślnie dodano nową wiadomość.");
    }, function (e) {
        toaster.pop('error', "Dodawanie wiadomości", "Nastąpił błąd podczas dodawania nowej wiadomości.");
    });
};

AddMessageToTopic()

[HttpPut]
public JsonResult AddMessageToTopic(OfferClientReponse response, int topicId, string messageText)
{
    var newMessage = new InnerCommunicationMessage();
    var targetTopic = response.InnerCommunicationTopics[topicId];
    newMessage.id = targetTopic.Messages.Count;
    newMessage.Message = messageText;
    newMessage.WhenCreated = targetTopic.WhenLastChange = DateTime.Now;
    UserModel currentUser = (UserModel)Session["user"];
    newMessage.WhoCreated = currentUser.FullName;

    // I know this part is wrong.
    var filesCount = Request.Files.Count;
    if (filesCount > 0)
    {
        for (int i = 0; i < filesCount; ++i)
        {
            var file = Request.Files[i];
            if (file != null && file.ContentLength > 0)
            {
                var doc = new MessageDocument();
                doc.DocumentName = Path.GetFileName(file.FileName);
                doc.DocumentExtName = Path.GetExtension(file.FileName);
                doc.FileId = String.Format("{0}{1}{2}", doc.DocumentName, OfferClientReponse.GetGUID(), doc.DocumentExtName);
                newMessage.Attachments.Add(doc);
            }
        }
    }

    targetTopic.Messages.Add(newMessage);
    LeaseService.GetInstance().SaveOfferInContainer(response);

    return Json(response);
}

我現在正在尋找答案幾個小時,不知道如何將AngularJS腳本連接到MVC。 我希望在按下Nowa wiadomość按鈕時上傳這些文件。

我確實將工作文件保存到Azure方法。

先感謝您!

使用此代碼,我認為這會有所幫助

$scope.addNewMessage = function () {
$http({ method: 'POST', url: '/OfferClientData/AddMessageToTopic', data: { response: $scope.data, topicId: $scope.topicMsgsToShow, messageText: $scope.dataForNewMessage.MsgText } }).then(function (d) {
    $scope.data = d.data;
    toaster.pop('success', "Dodawanie wiadomości", "Pomyślnie dodano nową wiadomość.");
}, function (e) {
    toaster.pop('error', "Dodawanie wiadomości", "Nastąpił błąd podczas dodawania nowej wiadomości.");
});

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM