|
|
|
| |
|
VisualBasic & C++ & PowerBasic - Forum
|
|
|
|
|
|
Rick
Hero
Reged: 20:40
Beitrag: 929
Ort: Deutschland
|
Servus Lieblings VB Vereinigung:) Meld mich nach 783 Jahren mal wieder zurück;)
Hab viel gemacht in VB bis heute. Etliche Zilliarden Zeilen Code geschrieben;) Und jetzt ist es mal wieder so weit, ich brauche eure Hilfe:) Und zwar:
Ich habe ein kleines Upload Problem.
'Programm verschlüsselt eine Datei 'Datei wird auf Server geladen 'Datei wird wieder Runtergeladen 'Datei wird entschlüsselt
Da dieser vorgang ohne Probleme lief hab ich nie gemerkt dass es doch einen kleinen Fehler gibt.
Die entschlüsselte Datei kann ich nicht mehr öffnen.
Liegt nicht am Crypting sondern an dem weg auf den Server. Irgendwas ändert sich an der Datei.
Ich nehme an dass der Fehler am Upload liegt.
Wenn ich das ganze z.B. Verschlüssel, dann per FireFTP hochlade, wieder runterlade und dann entschlüssel funktioniert die Datei reibungslos.
In meinem Programm mach ich den Upload per WebClient:
Code:
'FTP File Upload ' FTP-Server Dim sFTPHost As String = "ftp://"domain.de/data/"
' Benutzername und Kennwort für den Server-Zugriff Dim sUserName As String = sServerName Dim sPassword As String = "password"
'Setze Zugriffsrechte upload1.Credentials = New Net.NetworkCredential(sUserName, sPassword)
'Upload Dim serverUri As New Uri(sFTPHost & vName) upload1.UploadFileAsync(serverUri, sLocalPath)
Ich gehe davon aus dass die Daten im ASCII Mode gesendet werden und deshalb wird die ganze Sache nicht klappen.
Im FireFTP in FireFox hab ich die einstellung auf Binary gestellt, und damit klappts ja wunderbar.
Wie bekomme ich denn das ganze mit dem WebClient hin? Wär echt super wenn mir da jemand ein wenig unter die Arme greifen kann:)
So erstmal wars das. Ganz ganz Liebe Grüße an alle aktiven und beständigen User:) Besonders an MiB und Lordchen;) Wir werden alle nicht jünger^^
-------------------- Es kommt nicht darauf an, wie gut man ist, sondern wie gut die Anderen sind;)
MfG Rick
|
|
|
VFensterB
Champion
Reged: 20:08
Beitrag: 2716
|
Hi Rick,
also der FTPWebRequest hat eine UseBinary-Eigenschaft, siehe MSDN. Probier's doch mal damit.
-------------------- MfG
VFensterB
[VB6]
|
|
|
Rick
Hero
Reged: 20:40
Beitrag: 929
Ort: Deutschland
|
Hi VFensterB:)
msdn ist mir bekannt, und ich wäre heilfroh wenn ich diese erreichen würde! Bei mir ist seit über einem jahr die Microsoft website nicht mehr erreichbar.
Also ich mir vor 3 Wochen die neue VS2010 gekauft habe, war ich das erste mal wieder auf der seite von dem laden;) keine stunde später gings wieder nicht mehr. das problem scheint bekannt, aber eine lösung habe ich bisher nicht gefunden. Habs sogar schon über einen anderen proxy-server versucht, aber keine chance in die msdn zu kommen. Auch merkwürdig, da ich das problem in meiner langjährigen pc zeit erst hab seit dem ich mir einen neuen laptop gekauft habe und da is auch das erste mal alles von ms original.
auf meinen früheren systemen liefs immer. und da war nicht viel an originalsoftware drauf;)
naja auf jedenfall weiss ich nicht weiter...ich brauch lediglich ne uploadfunktion wie die oben beschriebene und zwar als binary modus.
Das einfachste wäre der webclient und der muss das doch auch irgendwie hinbekommen.
Zudem kommt dass ich eine statusanzeige für den upload brauche. das klappt meines wissens beim FtpWebRequest nicht. oder seh ich das falsch?
Hab ich halt für den webclienten schon alles fertig nur das mit dem modus funktioniert nicht...der läds halt nicht als binary hoch.
naja bin um jede hilfe froh:) auch wenn jemand das ms problem kennt
-------------------- Es kommt nicht darauf an, wie gut man ist, sondern wie gut die Anderen sind;)
MfG Rick
Bearbeitet von Rick (22:02 02/07/2010)
|
|
|
VFensterB
Champion
Reged: 20:08
Beitrag: 2716
|
Hi Rick,
FtpWebRequest ist eine Unterklasse von WebRequest. Es sollte daher eigentlich keine Probleme oder notwendige Änderungen beim Umstellen geben... Nicht-MSDN-Beispiel
-------------------- MfG
VFensterB
[VB6]
|
|
|
Rick
Hero
Reged: 20:40
Beitrag: 929
Ort: Deutschland
|
Ok, das ist ja schonmal Super:)
Klappt jetzt mit dem Binary:)
Allerdings kann die Hochgeladene Datei nach upload nicht mehr gelöscht werden. Fehler: "Datei wird verwendet..." das Problem muss im Uploadcode liegen. Bevor der Upload aufgerufen wird, kann die Datei noch gelöscht werden.
Hoffe es hat jemand eine Idee
Hier der Code:
Code:
Imports System.Net Imports System.Threading Imports System.IO Imports System.Diagnostics Imports System.Windows.Forms
Public Class FtpState Private wait As ManualResetEvent Private m_request As FtpWebRequest Private m_fileName As String Private m_operationException As Exception = Nothing Private status As String
Public Sub New() wait = New ManualResetEvent(False) End Sub
Public ReadOnly Property OperationComplete() As ManualResetEvent Get Return wait End Get End Property
Public Property Request() As FtpWebRequest Get Return m_request End Get Set(ByVal value As FtpWebRequest) m_request = value End Set End Property
Public Property FileName() As String Get Return m_fileName End Get Set(ByVal value As String) m_fileName = value End Set End Property Public Property OperationException() As Exception Get Return m_operationException End Get Set(ByVal value As Exception) m_operationException = value End Set End Property Public Property StatusDescription() As String Get Return status End Get Set(ByVal value As String) status = value End Set End Property End Class
Public Class AsynchronousFtpUpLoader Shared LblStatus As Label Shared txtulStat As TextBox Shared txtulCpl As TextBox Shared Progress As ProgressBar Public Delegate Sub UpdateTextCallback(ByVal text As String) Public Delegate Sub UpdateProgressCallback(ByVal bytesRead As Long, ByVal bytesMaximum As Long)
'''' <summary>Length of file to upload</summary> Shared fileLength As Long
Public Shared Sub UpdateText(ByVal text As String) LblStatus.Text = text End Sub
Public Shared Sub UpdateProgress(ByVal bytesRead As Long, ByVal bytesMaximum As Long) Progress.Value = CInt(Math.Truncate(bytesRead * (CDbl(100) / CDbl(bytesMaximum)))) End Sub
Public Shared Sub Upload(ByVal target As Uri, ByVal fileName As String, ByVal username As String, ByVal password As String, ByVal lblStatus__1 As Label, ByVal progress__2 As ProgressBar) LblStatus = lblStatus__1 fileLength = New FileInfo(fileName).Length If LblStatus IsNot Nothing Then LblStatus.Invoke(New UpdateTextCallback(AddressOf UpdateText), New Object() {"upload ... / " & fileLength.ToString()}) End If
Progress = progress__2 If Progress IsNot Nothing Then Progress.[Step] = 1 Progress.Minimum = 0 Progress.Maximum = 100 Progress.Visible = True Progress.Invoke(New UpdateProgressCallback(AddressOf UpdateProgress), New Object() {5, fileLength}) End If
' Create a Uri instance with the specified URI string. ' If the URI is not correctly formed, the Uri constructor will throw an exception. Dim waitObject As ManualResetEvent
Dim state As New FtpState() Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest) request.UseBinary = True request.Method = WebRequestMethods.Ftp.UploadFile
' This example uses anonymous logon. ' The request is anonymous by default; the credential does not have to be specified. ' The example specifies the credential only to control how actions are logged on the server. request.Credentials = New NetworkCredential(username, password)
' Store the request in the object that we pass into the asynchronous operations. state.Request = request state.FileName = fileName
' Get the event to wait on. waitObject = state.OperationComplete
' Asynchronously get the stream for the file contents. request.BeginGetRequestStream(New AsyncCallback(AddressOf EndGetStreamCallback), state)
Dim done As Boolean = False Do done = waitObject.WaitOne(200, True) Application.DoEvents() Loop While Not done
' The operations either completed or threw an exception. If state.OperationException IsNot Nothing Then Throw state.OperationException End If End Sub
Private Shared Sub EndGetStreamCallback(ByVal ar As IAsyncResult) Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)
Dim requestStream As Stream = Nothing ' End the asynchronous call to get the request stream. Try requestStream = state.Request.EndGetRequestStream(ar) ' Copy the file contents to the request stream. Const bufferLength As Integer = 2048 Dim buffer As Byte() = New Byte(bufferLength - 1) {} Dim count As Integer = 0 Dim readBytes As Integer = 0 Dim stream As FileStream = File.OpenRead(state.FileName) Do readBytes = stream.Read(buffer, 0, bufferLength) requestStream.Write(buffer, 0, readBytes) count += readBytes If LblStatus IsNot Nothing Then LblStatus.Invoke(New UpdateTextCallback(AddressOf UpdateText), New Object() {count.ToString() & " / " & fileLength.ToString()}) 'txtulStat.Invoke(New UpdateTextCallback(AddressOf UpdateText), New Object() {count.ToString()}) 'txtulCpl.Invoke(New UpdateTextCallback(AddressOf UpdateText), New Object() {fileLength.ToString()}) End If If Progress IsNot Nothing Then Progress.Invoke(New UpdateProgressCallback(AddressOf UpdateProgress), New Object() {CLng(count), fileLength}) End If Loop While readBytes <> 0
' IMPORTANT: Close the request stream before sending the request. requestStream.Close() ' Asynchronously get the response to the upload request. state.Request.BeginGetResponse(New AsyncCallback(AddressOf EndGetResponseCallback), state) ' Return exceptions to the main application thread. Catch e As Exception 'AsynchronousFtpUpLoader.txtStatus.AppendText("Could not get the request stream."); state.OperationException = e state.OperationComplete.[Set]() Return End Try End Sub
' The EndGetResponseCallback method completes a call to BeginGetResponse. Private Shared Sub EndGetResponseCallback(ByVal ar As IAsyncResult) Dim state As FtpState = DirectCast(ar.AsyncState, FtpState) Dim response As FtpWebResponse = Nothing Try response = DirectCast(state.Request.EndGetResponse(ar), FtpWebResponse) response.Close() state.StatusDescription = response.StatusDescription ' Signal the main application thread that the operation is complete. state.OperationComplete.[Set]() ' Return exceptions to the main application thread. Catch e As Exception LblStatus.Invoke(New UpdateTextCallback(AddressOf UpdateText), New Object() {e.Message})
state.OperationException = e state.OperationComplete.[Set]() End Try End Sub End Class
Aufrufen tut man das ganze mit: Code:
AsynchronousFtpUpLoader.Upload(sFTPUri, FilePath, Username, Password, lblProgress, ProgressBar1)
Lg Rick
-------------------- Es kommt nicht darauf an, wie gut man ist, sondern wie gut die Anderen sind;)
MfG Rick
|
|
0 registrierte und 1 anonyme Benutzer betrachten dieses Forum.
Moderator: Claus, MiB, VFensterB, Lordchen, Helmut
drucke Thema
|
Rechte
Du kannst keine neue Nachrichten schreiben
Du kannst keine Antworten schreiben
HTML ist deaktiviert
UBBCode ist aktiv
|
Bewertung:
Thema gelesen: 328
|
|
|
|
|
|

UBB.threads™ 6.5.1
|