locked
To know the size of an attachment RRS feed

  • Question

  • Hi

    InfoPath 2013

    I have Attachment field in my list, maximum size should be  5MB  and after 3 months it should get deleted. How can I achieved this.

    I found this script but I don't know how to apply it a. because InfoPath there no validation rule for attachment field and in properties of the field is disable. this is the code I found

    File size limits can be implemented on the form itself. You need to add validation to the field:

    1. Open the Data Source pane
    2. Double-click on field1 to bring up its properties
    3. In Validation, add a new condition
    4. Select Expression, type this expression in: string-length(.) > 136533
    5. Enter the text to be displayed when the error condition occurs

    Now when you edit the form, if the image is too big, you will get an error validation and the user will be warned on save.

    I haven’t tested this and it will need checking with multiple attachments. The above figure of 136533 is for a 100KB limit. To calculate the relevant figure:

    ExpressionLimit = KB * 1024 / 0.75

    Thanks

    Nilu

    Tuesday, March 21, 2017 10:01 AM

All replies

  • Hi,

    I checked in my local and found the Attachments field does not support validation.

    Here is one workaround to validate Attachments size in SharePoint form.

    SharePoint will do client side validation by PreSaveAction function, here is the sample code for your reference( insert script editor web part to new form and add the code to it).

    <script type="text/javascript">
            function PreSaveAction() {
                $('#idAttachmentsTable').find('tr').each(function () {
                    var filepath = $(this).find('span[dir="ltr"]').text();
                    alert(filepath);
                    var myFSO = new ActiveXObject("Scripting.FileSystemObject");                
                    var thefile = myFSO.getFile(filepath);
                    var size = thefile.size;
                    alert(size + " bytes");
                })            
            }
        </script>

     

    You may face the error(Scripting.FileSystemObject Automation server can't create object), in the case, you could add the site to trusted site and enable below option.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 22, 2017 6:38 AM
  • Hi  Lee

    please provide me with the step by stem description.

    thanks

    Nilu

    Wednesday, March 22, 2017 9:55 AM
  • Hi,

    Go to your list, click ‘new item’ to open the list new form.

    Then click site setting->Edit page->Add a web part->Script Editor

    Edit snippet and insert the code to it, click stop editing to save the page.

    <script type="text/javascript">
            function PreSaveAction() {
                var Check;
                $('#idAttachmentsTable').find('tr').each(function () {
                    var filepath = $(this).find('span[dir="ltr"]').text();
                    //alert(filepath);
                    var myFSO = new ActiveXObject("Scripting.FileSystemObject");                
                    var thefile = myFSO.getFile(filepath);
                    var size = thefile.size;
                    if (size > 100* 1024) { //100kb for test purpose
                        alert('file size bigger than 5MB');
                        return false;
                    } else {
                        Check = true;
                    }
                })
                return Check;
            }
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 22, 2017 10:28 AM
  • Hi lee

    I don't know to apply this

    nilu

    Wednesday, March 22, 2017 10:51 AM
  • Hi,

    You could try with above steps and list your detail steps if any issue, so we could know how to assist you.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 22, 2017 11:34 AM
  • Hi lee

    Highly appreciate the solutions sent without any hesitation.

    I add the web part and when I attached the file and save it I get this error.

    -------------------------------------------------------------------------------------------

    An error occurred while using the form. For more information, contact the site administrator.

    An entry has been added to the Windows event log of the server.

    Log ID:5567

    Correlation ID:fdbae09d-104a-4000-22dc-f8707ab0d257

    -------------------------------------------------------------------------------------------

    the script that I add was the one u sent first

    <script type="text/javascript">
            function PreSaveAction() {
                $('#idAttachmentsTable').find('tr').each(function () {
                    var filepath = $(this).find('span[dir="ltr"]').text();
                    alert(filepath);
                    var myFSO = new ActiveXObject("Scripting.FileSystemObject");               
                    var thefile = myFSO.getFile(filepath);
                    var size = thefile.size;
                    alert(size + " bytes");
                })           
            }
        </script>

    Is this error is due to this script or for some other field in the list

    Thank you

    Nilu

    Thursday, March 23, 2017 5:05 AM
  • Hi,

    The script is for SharePoint list form, I list the detail steps for your reference:

    Custom list with Attachments Enabled.

    Click ‘new item’ to open list new form.

    Click ‘Edit page’

    Add web part.

    Edit snippet

    Insert script

    Save page

    Result:

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 23, 2017 9:45 AM
  • Hi,

    Would you please provide us with an update on the status of your issue?

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, and we can learn from each other.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 27, 2017 10:00 AM
  • Hi Lee

    I did as you said

    what I put in webpart is

    <script type="text/javascript">
            function PreSaveAction() {
                var Check;
                $('#idAttachmentsTable').find('tr').each(function () {
                    var filepath = $(this).find('span[dir="ltr"]').text();
                    //alert(filepath);
                    var myFSO = new ActiveXObject("Scripting.FileSystemObject");               
                    var thefile = myFSO.getFile(filepath);
                    var size = thefile.size;
                    if (size > 100* 1024) { //100kb for test purpose
                        alert('file size bigger than 5MB');
                        return false;
                    } else {
                        Check = true;
                    }
                })
                return Check;
            }
        </script>

    I insert an attachment as shown below, when saving I got the below error message all mandatory fields are entered does the field name have to be in the script?

    Thanks

    Nilu



    Friday, March 31, 2017 11:17 AM
  • Hi,

    Try to add the jQuery library reference( I test this in my on-premise environment and I add the jQuery to my master page so I haven’t reference it).

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
            function PreSaveAction() {
                var Check;
                $('#idAttachmentsTable').find('tr').each(function () {
                    var filepath = $(this).find('span[dir="ltr"]').text();
                    //alert(filepath);
                    var myFSO = new ActiveXObject("Scripting.FileSystemObject");                
                    var thefile = myFSO.getFile(filepath);
                    var size = thefile.size;
                    if (size > 100* 1024) { //100kb for test purpose
                        alert('file size bigger than 5MB');
                        return false;
                    } else {
                        Check = true;
                    }
                })
                return Check;
            }
        </script>

    Or try to debug the code by developer tool( IE->F12).

    https://msdn.microsoft.com/en-us/library/gg589507(v=vs.85).aspx

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 3, 2017 8:17 AM
  • Hi Lee

    I still get the same error message I think I iam not applying this properly.when I save this only this error occurs

    do I have to submit it .error is 5567.do you know any video which explains this in more detail,

    Thank you for assisting me step by step.

    Regards,

    Nilukshi   

    Tuesday, April 4, 2017 7:23 AM
  • Hi,

    I just found the attachment is different with default, have you custom your list form?

    If you custom your list form, you need to implement the logic based on your case.

    The technology solution is grab the file path of the attachments and then read the size of the files.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 4, 2017 8:07 AM