2

I have a form in witch I can link one or many documents (the filepath and a name) (pdf, excel, pictures, kml) to a Land object. I don't know what is the best way to handle this, I searched and found many ways of doing this but it seems like none of them is the right way. I can't do it in AJAX since my object is not saved in the database at the creation of a new one. If I post the form and there's an error, the view is returned but my "uploaded" documents will be cleared (since it's file input). Also, how should I handle the edit of this object to show my list of documents and allow to delete/add other or edit only the name?

Is there any file handler existing, I've seen http://nuget.org/List/Packages/microsoft-web-helpers but it does not look very interesting.

2
  • Have you looked here: haacked.com/archive/2010/07/16/… Commented Jul 26, 2011 at 19:57
  • Yes, with this solution I still have the problem that the file input will be cleared when the form is invalid, and it does not help me with the handling of the editing of my object, and also, it does not help me find a good solution to name the files (save them in the database with a name(friendly name by the user) and a path) Commented Jul 26, 2011 at 20:04

2 Answers 2

2

telerik ASP.NET MVC upload extension is nice. http://demos.telerik.com/aspnet-mvc/razor/upload

Sign up to request clarification or add additional context in comments.

3 Comments

It seems to be a good control, I already use telerik for the grid in some project and I like it, did know about this control yet. Seems only one problem with what I want to do, How can I add a friendly name to a file? And is it possible to load this with a list of files when I want to edit my object?
hello, could you please follow this sample? demos.telerik.com/aspnet-mvc/razor/upload/syncupload i think that you can change file name ProcessSubmit.
telerik.com/community/forums/aspnet-mvc/upload/… I think that on the edit it's not possible to populate the list.
2

I have tried plupload and jquery file upload

Jquery file upload worked best as plupload messed around with the form submission and i couldnt get the anti forgery token to work (yet!).

You may also want to consider uploadify, but i havent tried that myself.

The telerik library is only free for certain projects, such as open source/ non commercial.

Here is the code i put together to use with the jQuery file upload...

 @section Header
{
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/themes/base/jquery-ui.css"
        id="theme" />
    <link href="/Content/plugins/jqUpload/files/jquery.fileupload-ui.css" rel="stylesheet"
        type="text/css" />
    <link href="/Content/plugins/jqUpload/style.css" rel="stylesheet" type="text/css" />
}
<div id="fileupload" class="grid_24">
    @using (@Html.BeginForm("Upload", "Photo", new { Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
 { 
        @Html.AntiForgeryToken()
        <div class="fileupload-buttonbar">
            <label class="fileinput-button">
                <span>Add files...</span>
                <input type="file" name="files" multiple="multiple" />
            </label>
            <button type="submit" class="start">
                Start upload</button>
            <button type="reset" class="cancel">
                Cancel upload</button>
            <button type="button" class="delete">
                Delete files</button>
        </div> 
 }
    <div class="fileupload-content">
        <table class="files">
        </table>
        <div class="fileupload-progressbar">
        </div>
    </div>
</div>
<div class="clear"></div>
<script id="template-upload" type="text/x-jquery-tmpl">
    <tr class="template-upload{{if error}} ui-state-error{{/if}}">
        <td class="preview"></td>
        <td class="name">${name}</td>
        <td class="size">${sizef}</td>
        {{if error}}
            <td class="error" colspan="2">Error:
                {{if error === 'maxFileSize'}}File is too big
                {{else error === 'minFileSize'}}File is too small
                {{else error === 'acceptFileTypes'}}Filetype not allowed
                {{else error === 'maxNumberOfFiles'}}Max number of files exceeded
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="progress"><div></div></td>
            <td class="start"><button>Start</button></td>
        {{/if}}
        <td class="cancel"><button>Cancel</button></td>
    </tr>
</script>
<script id="template-download" type="text/x-jquery-tmpl">
    <tr class="template-download{{if error}} ui-state-error{{/if}}">
        {{if error}}
            <td></td>
            <td class="name">${name}</td>
            <td class="size">${sizef}</td>
            <td class="error" colspan="2">Error:
                {{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
                {{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
                {{else error === 3}}File was only partially uploaded
                {{else error === 4}}No File was uploaded
                {{else error === 5}}Missing a temporary folder
                {{else error === 6}}Failed to write file to disk
                {{else error === 7}}File upload stopped by extension
                {{else error === 'maxFileSize'}}File is too big
                {{else error === 'minFileSize'}}File is too small
                {{else error === 'acceptFileTypes'}}Filetype not allowed
                {{else error === 'maxNumberOfFiles'}}Max number of files exceeded
                {{else error === 'uploadedBytes'}}Uploaded bytes exceed file size
                {{else error === 'emptyResult'}}Empty file upload result
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="preview">
                {{if thumbnail_url}}
                    <a href="${url}" target="_blank"><img src="${thumbnail_url}"></a>
                {{/if}}
            </td>
            <td class="name">
                <a href="${url}"{{if thumbnail_url}} target="_blank"{{/if}}>${name}</a>
            </td>
            <td class="size">${sizef}</td>
            <td colspan="2"></td>
        {{/if}}
        <td class="delete">
            <button data-type="${delete_type}" data-url="${delete_url}">Delete</button>
        </td>
    </tr>
</script>
@section Scripts
{
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
    <script src="/Content/plugins/jqUpload/files/jquery.iframe-transport.js" type="text/javascript"></script>
    <script type="text/javascript" src="/Content/plugins/jqUpload/files/jquery.iframe-transport.js"></script>
    <script type="text/javascript" src="/Content/plugins/jqUpload/files/jquery.fileupload.js"></script>
    <script type="text/javascript" src="/Content/plugins/jqUpload/files/jquery.fileupload-ui.js"></script>
    <script src="/Content/plugins/jqUpload/application.js" type="text/javascript"></script>
}

and the controller...

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Upload(int? id, IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = id + "_" + Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                }
            }
            return Json(new {name = fileName, type = "image/jpeg"});
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.