AddAttachment Function of SPServices (List AddAttachment)

Dec 12, 2013 at 2:42 PM
Hi,

I have developed a project on sharepoint where I have used a custom form to Add/Update a list using SPServices. Now as per some user requirement I need to provide the option to add an attachment to the list. Though while testing I am able to get the attachment name and also able to delete it, but I am not able to upload any attachment using operation: "AddAttachment" option. I am not able to find any documentation on the same too. Is there any way I can perform this action. User needs to browse a file from the system and attach it. I followed this by bjwildbore post but it needs a updated browser which my user may or may not have.

I am confused how to provide the fileName and attachment details. Can be the file name be "C:\Myfile.docx"? and what will be the attachment details? Please help.
Coordinator
Dec 12, 2013 at 4:45 PM
I was going to point you to bjwildbore's post, but you've already tried it. What browsers do you need to target and what version of SharePoint, jQuery, and SPServices are you using?

M.
Dec 12, 2013 at 6:02 PM
Edited Dec 12, 2013 at 6:25 PM
I have downloaded the latest version SPServices 2013.01 & jquery-1.10.2.min. And my target browser is IE8 mostly.

And regarding bjwildbore's post, Why event.target.files is returning undefined? When I tried event.target.name it showed "attachmentButton". But with event.target.files its undefined and files.length shows null or not an object.
Coordinator
Dec 12, 2013 at 6:14 PM
I'd suggest you stick with the 1.x branch of jQuery, but that's a different discussion.

As you can see from the caniuse link Brendan posted, IE8 doesn't support fileReader.

M.
Dec 12, 2013 at 6:37 PM
Thanks M,

Any resolution to my original question.
Coordinator
Dec 12, 2013 at 6:49 PM
Sorry; I sort of left you flat.

The trick is to get the base-64 byte array for the file. That's probably where you are going to have problems with IE8.

The filename ought to be just the filename, not any part of the path. It's the name which is stored for the attachment on the item.

M.
Dec 13, 2013 at 1:28 PM
Thank You Again M,

Any reference for base-64 byte array for the file I can follow?
Coordinator
Dec 13, 2013 at 2:36 PM
Nope. I've never really tried to get uploads working because I don't know of a reliable way to do it cross-browser.

M.
Dec 13, 2013 at 3:13 PM
If you search this forum for "Base64" I'm sure you will find something including a function that will do the conversion or a pointer to a script that you can download that will do such conversions.

A few things you should understand about file uploads and the current client side api services available:

1. They will NOT retrieve the file from the users computer. That is up to you (the developer) to do. With browsers that support HTML5 file api, your are in luck: you can get the data of the user selected file, encode it to Base64 (if needed) and then use the SP api service to send that data to SP and create the file.

2. I have not seen any examples of the file service being used on a list item attachment. Maybe it's possible, but all the exams I have seen were for documents in a document library.
Good luck. And post back if you find a solution to your use case.

Paul.



--

_________
Paul T