Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi I am uploading file from asp.net 3.5 to sharepoint server 2010.
My code is give below


WebResponse response = null;

try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(SharePointPath);

request.Credentials = CredentialCache.DefaultNetworkCredentials;

request.Method = "PUT";

// Allocate a 1 KB buffer to transfer the file contents.
// You can adjust the buffer size as needed, depending on
// the number and size of files being uploaded.
byte[] buffer = new byte[1029120];

// Write the contents of the local file to the
// request stream.
using (Stream stream = request.GetRequestStream())
using (FileStream fsWorkbook = File.Open(UploadedFilePath,
FileMode.Open, FileAccess.Read))
{
int i = fsWorkbook.Read(buffer, 0, buffer.Length);

while (i > 0)
{
stream.Write(buffer, 0, i);
i = fsWorkbook.Read(buffer, 0, buffer.Length);
}
}

// Make the PUT request.

response = request.GetResponse();
}
catch (Exception ex)
{
L.Text = ex.Message.ToString() + "" + ex.Source.ToString() + "" + ex.StackTrace.ToString();
}
finally
{
// response.Close();
}

Its throw error is
The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.GetResponse()

Please help & what is issue ?

Thanks
Posted
Comments
jkirkerx 17-Dec-12 0:09am    
wouldn't it be while (i < buffer.length)
ganusharma 17-Dec-12 0:15am    
thank for quick reply.
your means problem in code. you have exact solution for this problem.
excually i am new dotnet. problem is uploading file asp.net to sharepoint server.

Please Reply.
thanks.
jkirkerx 17-Dec-12 0:25am    
Not at the moment. You code is kind of confusing to me.

I think it's bombing on the stream.write in the while loop. You can delete that code and run it to see if that clears the error 500.

I can post some sample code in vb that reads a file stream in bytes, so the bytes can be manipulated or altered.

1 solution

This will read the stream 1 byte at a time, regardless of size. You can check for certain bytes to omit them, like the BOM marker. It reads 1 byte at a time, and when it reaches the linefeed, it creates a line array of bytes. You can just loop the bdx to build a byte array. I think this is what your trying to do, but I'm not clear on your intentions.

But you should get the principle of reading a file at the more advanced level, in which you can work and alter the data.

Sorry about the VB, that what I write in. the principle should be the same in c-sharp.

'Read the Html Template from App_Data, and format the template fields
            htmlStream = New FileStream(m_PhysicalPath, FileMode.Open, FileAccess.Read)
            Dim htmlLen As Long = htmlStream.Length
            Dim fileData As Byte() = New Byte(htmlLen) {}
            htmlStream.Read(fileData, 0, htmlLen)

            If Not (htmlStream Is Nothing) Then htmlStream.Close()
            
            Dim byteLine() As Byte = New Byte() {}
            Dim strArray() As String = New String() {}
            For bdx As Integer = 0 To fileData.Length - 1

                Dim byteVal As Byte = fileData(bdx)
                If Not (byteVal = &HD) Then

                    Array.Resize(byteLine, byteLine.Length + 1)
                    byteLine(byteLine.Length - 1) = byteVal

                Else
                    Array.Resize(byteLine, byteLine.Length + 1)
                    byteLine(byteLine.Length - 1) = byteVal

                    Dim charLine() As Char = New Char() {}
                    Array.Resize(charLine, byteLine.Length + 1)

                    For cdx As Integer = 0 To byteLine.Length - 1
                        charLine(cdx) = AsciiByteToChar(byteLine(cdx))
                    Next

                    Dim value As String = New String(charLine)
                    Array.Resize(strArray, strArray.Length + 1)
                    strArray(strArray.Length - 1) = value

                    Array.Resize(byteLine, 0)
                    bdx += 1

                End If
            Next

            '//////////////////////////////////////////////////////////////////
            ' Remove the BOM Marker
            strArray(0) = Right(strArray(0), strArray(0).Length - 3)
            '//////////////////////////////////////////////////////////////////
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900