SPWeb.GetFileAsString(URL) causing the problem, and throws COMException, if an XML file size is more than 10 MB


PROBLEM : SPWeb.GetFileAsString(URL) method throws COMException, if we pass XML file and its size is more than 10 MB


WORK AROUND :


Instead of using SPWeb.GetFileAsString(URL), use SPFile.OpenBinary() method.


try {
string str = Url.Text;
using (SPSite portalSite = new SPSite(str)){
SPWeb web = portalSite.OpenWeb();
SPFile file = web.GetFile(str);
byte[] content = file.OpenBinary();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str1 = enc.GetString(content);
//string str1 = web.GetFileAsString(str);
MessageBox.Show("File fetched successfully");
}
}
catch(Exception ex){
MessageBox.Show(ex.ToString());
}

Comments (3)
  1. DkmS says:

    I use that code:

    public static string TextOfFile(SPFile file) {

    byte[] bf = file.OpenBinary();

    if(file.CharSetName!=null)

    return Encoding.GetEncoding(file.CharSetName).GetString(file.OpenBinary());

    else

    return Encoding.UTF8.GetString(file.OpenBinary());

    }

  2. public static string GetString(SPFile file)

    {

    using(var reader = new StreamReader(file.OpenBinaryStream()))

                           {

                               return reader.ReadToEnd();

                           }

    }

Comments are closed.

Skip to main content