Freigeben über


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

  • Anonymous
    December 08, 2005
    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());
    }

  • Anonymous
    June 14, 2009
    PingBack from http://patiocushionsource.info/story.php?id=2227

  • Anonymous
    September 12, 2013
    public static string GetString(SPFile file) { using(var reader = new StreamReader(file.OpenBinaryStream()))                        {                            return reader.ReadToEnd();                        } }