?
Solved

Problem Uploading files with Webclient.uploadfile()

Posted on 2005-05-17
8
Medium Priority
?
2,833 Views
Last Modified: 2010-08-05
I'm trying to write a program to allow a user to select a file(in particular image files) and upload it to a remote server.  Currently I'm to use the webclient uploadfile method.  If I use it with the "POST" verb I get a eror from the server: method not allowed.  I've tried the "PUT" verb as suggested by another question here at experts=exchange.  The file gets uploaded, but can no longer be opened, there are errors in the file.  I tried a textfile as well, it could be opened but there was added text.  Any help is as always appreciated.  I'm also not set in stone using uploadfile so if you have another way of uploading the files I'm willing to try them.  Thanks in advance.

~pcjunky
0
Comment
Question by:pcjunky
  • 4
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
prakash_prk earned 1000 total points
ID: 14025918
Try this plz

----------------------------------------------------------------------------------------------------------------------------------------------
Friend Function UploadFile(ByVal SourceLocation As String, ByVal DestinationLocation As String, ByRef ErrorMessage As String, ByRef ProgressBar As System.Windows.Forms.ProgressBar) As Boolean
        Dim StartTime As DateTime = DateTime.Now()
        Dim Response As String = Nothing, FileSize As Double = 0
        Try
            UploadFile = False
            If SourceLocation.ToString.Trim <> "" And DestinationLocation.ToString.Trim <> "" Then
                'To set Upload settings
                Dim UploadRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(New Uri(DestinationLocation.ToString.Trim)), System.Net.HttpWebRequest)
                UploadRequest.Timeout = 60000000
                UploadRequest.Method = "PUT"
                UploadRequest.ContentLength = New System.IO.FileInfo(SourceLocation.ToString.Trim).Length
                FileSize = UploadRequest.ContentLength.ToString.Trim

                'To write source & Target locations on log file
                Settings.WriteToLog("Local file  :" & SourceLocation.ToString.Trim & ".", "UploadFile", "clsUploadDownload", False)
                Settings.WriteToLog("Remote file :" & DestinationLocation.ToString.Trim & ".", "UploadFile", "clsUploadDownload", False)
                If FileSize > 1024 * 1024 Then
                    Settings.WriteToLog("File Size   :" & Microsoft.VisualBasic.FormatNumber((FileSize / 1024) / 1024, 2).ToString.Trim & " MB.", "UploadFile", "clsUploadDownload", False)
                ElseIf FileSize > 1024 Then
                    Settings.WriteToLog("File Size   :" & Microsoft.VisualBasic.FormatNumber(FileSize / 1024, 2).ToString.Trim & " KB.", "UploadFile", "clsUploadDownload", False)
                Else
                    Settings.WriteToLog("File Size   :" & Microsoft.VisualBasic.FormatNumber(FileSize, 2).ToString.Trim & " bytes.", "UploadFile", "clsUploadDownload", False)
                End If

                'To set Upload Stream settings
                Dim SourceStream As New System.IO.FileStream(SourceLocation.ToString.Trim, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)
                Dim RequestStream As System.IO.Stream = UploadRequest.GetRequestStream()
                Dim Buffer(4095) As Byte
                Dim Position As Integer = 0, ivlLoop As Integer = 0, CurLocation As Integer = 0
                Position = SourceStream.Read(Buffer, 0, Buffer.Length)
                ProgressBar.Visible = True
                ProgressBar.Value = 0
                ProgressBar.Maximum = 100
                While Position <> 0
                    RequestStream.Write(Buffer, 0, Position)
                    Position = SourceStream.Read(Buffer, 0, Buffer.Length)
                    CurLocation += Position

                    ProgressBar.Value = CInt((CurLocation / FileSize) * 100)
                    ProgressBar.Refresh()
                End While

                'To upload Stream on Remote system
                Dim WebResponse As System.Net.HttpWebResponse = CType(UploadRequest.GetResponse(), System.Net.HttpWebResponse)
                Dim ResponseReader As New System.IO.StreamReader(WebResponse.GetResponseStream())
                Response = ResponseReader.ReadToEnd()
                UploadFile = True
                Call Settings.WriteToLog("Status      :File uploaded successfully.", "UploadFile", "clsUploadDownload", False)
                Call Settings.WriteToLog("Time Taken  :" & Microsoft.VisualBasic.FormatNumber(System.DateTime.op_Subtraction(DateTime.Now, StartTime).TotalSeconds, 2).ToString.Trim & " seconds.", "UploadFile", "clsUploadDownload", False)

                RequestStream.Close()
                UploadRequest = Nothing
                SourceStream = Nothing
                RequestStream = Nothing
                WebResponse = Nothing
                ResponseReader = Nothing
            ElseIf SourceLocation.ToString.Trim = "" Then
                Call Settings.WriteToLog("Source Location is missing", "UploadFile", "clsUploadDownload", False)
            ElseIf DestinationLocation.ToString.Trim = "" Then
                Call Settings.WriteToLog("Destination Location is missing", "UploadFile", "clsUploadDownload", False)
            End If
        Catch ex As Exception
            Call CommonFunctions.HandleWebException(DestinationLocation, "UploadFile", "clsUploadDownload", ErrorMessage, ex)
            Call Settings.WriteToLog("Status      :" & ex.Message.ToString & "[" & ex.Source.ToString & "]", "UploadFile", "clsUploadDownload", False)
            If ErrorMessage.ToString.Trim = "" Then
                Call Settings.WriteToLog("Time Taken  :" & Microsoft.VisualBasic.FormatNumber(System.DateTime.op_Subtraction(DateTime.Now, StartTime).TotalSeconds, 2).ToString.Trim & " seconds.", "UploadFile", "clsUploadDownload", False)
            End If
        End Try
        ProgressBar.Visible = False
    End Function
----------------------------------------------------------------------------------------------------------------------------------------------
regards
prakash
0
 

Author Comment

by:pcjunky
ID: 14028528
Thanks parkash.  That seemed to do the trick.  The only issue I'm having is once I've uploaded the file I try to navigate to it with the web browser control on my form, but sometimes it won't show the picture and instead opens with windows picture and fax viewer.

Thanks again.
~pcjunky
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 14033476
oh sorry
I forgot to remove the codes
"settings.writetolog()... Which is simple a log written to a file...."
"the functions takes an arguament as progressbar and show the download status also...

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:prakash_prk
ID: 14033477
can u say what is type of file u uploaded ( gir or jpg)....

regards
prakash
0
 

Author Comment

by:pcjunky
ID: 14036024
It's a jpg in both cases.  I tested it with the sample pictures that come with Windows (winter, sunset, etc).  The sunset.jpg works fine, but winter.jpg opens like I said.

~pcjunky
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 14042964
While you try to view that file in the browser, the image is not shown instead of that it opens fax or picture viewer !!!!

Is the Image shwon in pictureviewer??

regards
prakash
0
 

Author Comment

by:pcjunky
ID: 14045449
Yes when I try to navigate to the picture with the web browser control in my program it opens picture viewer instead of navigating to it.  The image is shown in picture viewer.

~pcjunky
0
 

Expert Comment

by:DonGarry
ID: 15118490
Hello Prakash, this post looks to be what I'm looking for. ...plus is looks like it has some excellent features like tracking progress. I'm trying to upload a local file to a hosted server without user intervention.

I'm a newbie trying to figure out the above code. I'm using Web Matrix and VB.

In the above code how would I load the source location, destination location, source filename and destination filename ?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question