Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using FileUploader and a view model to post
Message
 
 
General information
Forum:
ASP.NET
Category:
MVC
Environment versions
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Miscellaneous
Thread ID:
01648596
Message ID:
01648649
Views:
25
Hi Viv,

With some help of my colleague I was able to solve that problem. My js code is now
 if ($scope.attachments.body.rows.length >0 )
                    {
                        var files = [];
                        $.each($scope.attachments.body.rows, function (index, fileInfo) {                            
                            files.push(fileInfo.content);
                        });
                        Upload.upload({
                            url: 'api/EMessage/UploadFilesWithData',
                            multiple: true,
                            data: {
                                files: files,
                                data: Upload.json(eMessage)
                            }
                        }).then(function (response) {
                            //$timeout(function () {
                            //    $scope.result = response.data;
                            //});
                            var alertMessage = resourceFactory.getResource('Messages', 'sendingProcessCompleted');
                            toastr.clear();
                            toastr.success(alertMessage);

                            $scope.disableAction = false;
                            showSearchResults();
                            return;
                        }, function (response) {
                            if (response.status > 0) {
                                $scope.errorMsg = response.status + ': ' + response.data;
                            }
                        }, function (evt) {
                            $scope.progress =
                                Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                        });
                        
                    }
and the API is very similar to what I had originally in the Upload method. I only tested with Chrome so far, but I hope that files multiple works for other browsers as well.


>>Hi everybody,
>>
>>I am wondering if anyone implemented the File Uploader with the ASP.NET MVC and Windows API. I'm having problems :(
>>
>>My last attempt is the following:
>>
>>I created this class
>>
>>
>> public class FileUploadResult
>>    {
>>        public String FileName { get; set; }
>>        public String Key { get; set; }
>>        public int PercentComplete { get; set; }
>>        public HttpPostedFile Content { get; set; }
>>        
>>        public string Type { get; set; }
>>    }
>>
>>and in my JavaScript file I attempt to save file as the Content property. So far so good.
>>
>>However, I am unable to post that info using my model which is the following:
>>
>>
>> public class EditMessageViewModel
>>    {
>>        public short FormType { get; set; }
>>        public int MessageId { get; set; } // MessageID (Primary key)
>>        //public byte ReciptType { get; set; } // ReciptType. Type of recipient.  1=Guest.
>>        //public decimal ReciptId { get; set; } // ReciptID. ID of the recipient.  I.e. if recipient type is a guest, then guest_no.
>>        public string Body { get; set; } // Body
>>  //      public System.DateTime DateTime { get; set; } // date_time
>>  //      public System.Nullable<System.DateTime> Delivered { get; set; } // delivered
>>        public System.DateTime Expires { get; set; } // expires
>>
>>        public string MsgFrom { get; set; }
>>
>>        public string Subject { get; set; }
>>
>>        public List<FileUploadResult> Attachments { get; set; }
>>
>>        public List<GuestsList> Recipients { get; set; }
>>    }
>>
>>However, when I try to post I'm getting the 'Bad Request' right away, so apparently I can not post such model back.
>>
>>So, how exactly should I post both my model and the files I'm trying to upload?
>
>Multipart form data can contain a mix of files and (for example) json objects. But I haven't used the MVC FileUpoader and don't know whether you can add additional parts on the client side before posting.
>
>In any case I don't see how you can create a valid FileUploadResult in javascript since it contains a HttpPostedFile instance and one of it's properties is a .NET stream.......
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform