2

I have below code running on site http://url:94 on 94 port and I want to read the fileStream using openBinrayStream where file is kept on another site running on 92 port, even though I am giving the site url to spsite object it fails to access the file, and error is Cannot open file filename

using (SPSite prSiteObj = new SPSite(http://url:92))
                            {
                                using (SPWeb prweb = prSiteObj.OpenWeb())
                                {
                                    SPFile file = prweb.GetFile(http://url:92/pathtofile.pdf);
                                    Stream contentStream = file.OpenBinaryStream();
                                    var attachment = new MailAttachment(contentStream, file.Name);
                                    email.Attachments.Add(attachment);
                                    emailAttachmentLogBuilder.Append(file.Name + "|");

                                }
                            }

Is there any way to read file stream of file kept on different web application.

1 Answer 1

1

Okay, the mistake which I was doing is ignoring the URL after the port number, indeed the file was on different web application but it was in a subsite of the different web application. I had to modify the code slightly, like a below one.

using (SPSite prSiteObj = new SPSite(http://url:92))
                        {
                            using (SPWeb prweb = prSiteObj.OpenWeb("subsite-name"))
                            {
                                SPFile file = prweb.GetFile(http://url:92/pathtofile.pdf);
                                Stream contentStream = file.OpenBinaryStream();
                                var attachment = new MailAttachment(contentStream, file.Name);
                                email.Attachments.Add(attachment);
                                emailAttachmentLogBuilder.Append(file.Name + "|");

                            }
                        }

Now it's working.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.