2

quick question, I want to give the user the option to download all attachments from my details view.

This works for a single attachment...

    @foreach (var file in Model.Attachments)
            {
                <div class="margin-bottom-25">
                    <a href="?">
                        <img src="~/Images/pdf.gif" /></a>
                    <div>
                        <strong>@file.FileName</strong>
                        <span>@file.FileSizeKB KB</span>

                        @Html.ActionLink("Download", "ViewDocument", new
                           {
                               messageId = file.MessageId.ToString("N"),
                               documentId = file.ID.ToString("N"),
                               documentType = file.AttachmentType.ToString(),
                               download = true
                           })
                    </div>
                </div>

            }

however I want to give the user the ability to select a "Download all" link....? Sounds straight forward, but I tried putting the foreach within the link and it's a none runner.....any ideas appreciated ? (asp.net MVC 4 application, using razor2 views)

mkell
  • 711
  • 2
  • 13
  • 31
  • 3
    If you need to download all the files at the same time, your best option may be to download them all within a ZIP file; multi-part download is very badly supported. – Adrian Wragg Sep 04 '13 at 11:15
  • the idea is that these attachments will be very small, your saying I need to investigate a way of placing all in a Zip file before allowing download ? – mkell Sep 04 '13 at 11:22

2 Answers2

1

Iterate over all the documents and construct a zip file (best supported) that you then subsequently send to the user.

Just implement a different action on your controller, call it DownloadAll. Loop over all files in Model.Attachments, and add them to a ZIP package which you then return, just like Download does on your ViewDocument.

Take a look at ZipArchive http://msdn.microsoft.com/en-us/library/hh137435 if you use .NET 4.5 or otherwise ZipPackage. See How do I ZIP a file in C#, using no 3rd-party APIs? for more information too.

Community
  • 1
  • 1
ranieuwe
  • 2,268
  • 1
  • 24
  • 30
0

The 'correct' way to solve this problem would be by outputting your files using a "multipart" Content-Type, for example by outputting a header of

Content-Type: multipart-mixed

and then outputting each file individually with appropriate boundaries. However, support for this is very limited - in a similar question here browser support was shown to be very weak.

Therefore, as per my original comment your best route would be to use a library such as DotNetZip to combine all the files into a single ZIP file, allowing the users to download this instead.

Community
  • 1
  • 1
Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50