Currently I'm developing a webpart using SP2010 named ProfilePicture. I've successfully imported the webpart to SP2007. My problem is I receive an error whenever I attempt to upload a picture. I can display but cannot upload(replace). When I debug in my local 2010 environment no error occurs; everything running ok but in 2007 error:
The remote server returned an error: (401)Unauthorized.
The file is copied to the ~temp file but fails to copy to sharepoint document library.
What should I do? totally lost.
in local (80/Temp) ---> http://servername/ProfilePicture/name.jpg ---> no error
in other server(2007) -- > (80/Temp) --> http://servername/ProfilePicture/name.jpg --> unauthorized
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim MemberID As String = Request.QueryString("MemberID")
Image1.ImageUrl = "http://servername/ProfilePicture/" & RTrim(MemberID) & ".jpg"
End Sub
Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UploadButton.Click
Dim uploadedFilePath As String = Server.MapPath("~/Temp/")
Dim sharePointListPath As String = "http://servername/ProfilePicture/"
If FileUpload1.HasFile Then
Try
Dim fileName As String = FileUpload1.FileName.Replace(FileUpload1… Request.QueryString("MemberID")) & ".jpg"
FileUpload1.SaveAs(uploadedFilePath + fileName)
UploadFileToSharePoint(uploadedFilePath + fileName, sharePointListPath + fileName)
Label1.Text = "File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & FileUpload1.PostedFile.ContentLength & " bytes<br>" & _
"Content type: " & FileUpload1.PostedFile.ContentType & "<br>" & _
"Save To: " & sharePointListPath + fileName & " From: " & uploadedFilePath + fileName
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString() & "sharePointListPath: " & (sharePointListPath)
End Try
Else
Label1.Text = "You have not specified a file."
End If
End Sub
Sub UploadFileToSharePoint(ByVal UploadedFilePath As String, ByVal SharePointPath As String)
Dim response As WebResponse = Nothing
Try
' Create a PUT Web request to upload the file.
Dim request As WebRequest = WebRequest.Create(SharePointPath)
request.Credentials = New System.Net.NetworkCredential("user", "password", "domain")
request.PreAuthenticate = True
'request.Credentials = CredentialCache.DefaultCredentials
request.Method = "PUT"
Dim buffer() As Byte = New Byte(1023) {}
Using stream As IO.Stream = request.GetRequestStream()
Using fsWorkbook As FileStream = File.Open(UploadedFilePath, FileMode.Open, FileAccess.Read)
Dim i As Integer = fsWorkbook.Read(buffer, 0, buffer.Length)
Do While i > 0
stream.Write(buffer, 0, i)
i = fsWorkbook.Read(buffer, 0, buffer.Length)
Loop
End Using
End Using
' Make the PUT request.
response = request.GetResponse()
Catch ex As Exception
'Throw ex
Label2.Text = "ERROR2: " & ex.Message.ToString()
Finally
response.Close()
End Try
End Sub