How to upload image file into the SharePoint 2010 picture library using ECMA Script

2.3k views Asked by At

I need to upload image file into the sharepoint 2010 picture library using java script...

requirement is --

1.we have File Upload control

2.And, we have to upload image file from that file upload control

Please see code...But this code is not working (showing "Undefined object" exception for 'File' or 'FileInfo')

If any body have better solution that would be nice.

Thanks in advance.

<script>
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Test');
//var fStream = (new FileInfo(uploadimagepath)).OpenRead();
var fStream = File.OpenRead(uploadimagepath);
//var fStream = FileUpload.PostedFile.InputStream;
//var contents = new byte[fStream.Length];
var newPic = oList.RootFolder.Files.Add(phototitle, fStream);
var oItem = newPic.Item;
oItem.set_item('Title', phototitle);
oItem.update();
oList.Rootfolder.Update();
clientContext.load(oItem);
</script>
1

There are 1 answers

1
Chandra Sekhar Walajapet On

you cannot use the uploadimagepath, it gives you the path from the client.

instead use the file stream which is sent from client to server.

check this url for understanding the file upload control better

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx