close
參考 :
http://www.dotblogs.com.tw/puma/archive/2008/12/07/6289.aspx
http://www.cnblogs.com/anjou/archive/2007/12/07/986887.html
silverlight 版 webclient 資料上傳函式 用法 ' jk測試ok
'---資料上傳---------------
'法1:
'Dim array1 = ConvertStreamToBytes(pic_streamW)
'Dim data1 = Convert.ToBase64String(array1) '要先轉換成字串模式
'UploadData(upfileName, data1)
'---------------------------------
'法2:
UploadFile("tempPic_F.jpg", pic_streamW)
'----/資料上傳/----------------
'以下打包成一個Class類別庫 測試ok
'================================
Imports System.IO
Imports System.Net
Namespace ClassStream
Public Class ClassStream
Public Shared Function ConvertStreamToBytes(ByVal stream As Stream) As Byte()
Dim bytes() = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)
Return bytes
End Function
Public Shared Sub StreamToFile(ByVal stream As Stream, ByVal fileName As String)
' // 把 Stream 转换成 byte[]
Dim bytes = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)
' // 把 byte[] 写入文件
Dim fs = New FileStream(fileName, FileMode.Create)
Dim bw = New BinaryWriter(fs)
bw.Write(bytes)
bw.Close()
fs.Close()
End Sub
Public Shared Function FileToStream(ByVal fileName As String) As Stream
' // 打开文件
Dim fileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
' // 读取文件的 byte[]
Dim bytes() = New Byte(fileStream.Length) {}
fileStream.Read(bytes, 0, bytes.Length)
fileStream.Close()
' // 把 byte[] 转换成 Stream
' Dim stream = New MemoryStream(bytes)
Dim stream = BytesToStream(bytes)
Return stream
End Function
Public Shared Function BytesToStream(ByVal bytes As Byte()) As Stream
Dim stream = New MemoryStream(bytes)
Return stream
End Function
'//上傳檔案
Public Shared Sub UploadFile(ByVal savefileName As String, ByVal data As Stream)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功
Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim c = New WebClient()
'--------------事件委派動作---------------------------------------
'c.OpenWriteCompleted += (sender, e) =>
' {
' PushData(data, e.Result);
' e.Result.Close();
' data.Close();
' MessageBox.Show("上傳完畢");
' };
' vb對應的寫法如下
AddHandler c.OpenWriteCompleted, Sub(sender, e)
PushData(data, e.Result)
e.Result.Close()
data.Close()
End Sub
'------------/事件委派動作/---------------------------------------
c.OpenWriteAsync(ub.Uri, "POST")
End Sub
Public Shared Sub UploadData(ByVal savefileName As String, ByVal data_str As String)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功
Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim wc = New WebClient()
wc.UploadStringAsync(ub.Uri, data_str)
End Sub
Public Shared Sub PushData(ByVal input As MemoryStream, ByVal output As Stream)
Dim buffer() As Byte = New Byte(4000000) {} ' 4000K =4MB 暫存區
Dim stream As Stream = input
Dim read As Integer = 0
Dim total As Integer = 0
Do '回傳這次讀取了多少個 最多4096 個byte
read = stream.Read(buffer, total, 4096)
output.Write(buffer, total, read)
If (read <> 0) Then
total += read '累計總量
End If
Loop While (read <> 0)
'Dim bytesRead As Integer
'bytesRead = input.Read(buffer, 0, buffer.Length)
'While (input.Read(buffer, 0, buffer.Length)) <> 0
' output.Write(buffer, 0, bytesRead)
'End While
End Sub
End Class
End Namespace
http://www.dotblogs.com.tw/puma/archive/2008/12/07/6289.aspx
http://www.cnblogs.com/anjou/archive/2007/12/07/986887.html
silverlight 版 webclient 資料上傳函式 用法 ' jk測試ok
'---資料上傳---------------
'法1:
'Dim array1 = ConvertStreamToBytes(pic_streamW)
'Dim data1 = Convert.ToBase64String(array1) '要先轉換成字串模式
'UploadData(upfileName, data1)
'---------------------------------
'法2:
UploadFile("tempPic_F.jpg", pic_streamW)
'----/資料上傳/----------------
'以下打包成一個Class類別庫 測試ok
'================================
Imports System.IO
Imports System.Net
Namespace ClassStream
Public Class ClassStream
Public Shared Function ConvertStreamToBytes(ByVal stream As Stream) As Byte()
Dim bytes() = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)
Return bytes
End Function
Public Shared Sub StreamToFile(ByVal stream As Stream, ByVal fileName As String)
' // 把 Stream 转换成 byte[]
Dim bytes = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)
' // 把 byte[] 写入文件
Dim fs = New FileStream(fileName, FileMode.Create)
Dim bw = New BinaryWriter(fs)
bw.Write(bytes)
bw.Close()
fs.Close()
End Sub
Public Shared Function FileToStream(ByVal fileName As String) As Stream
' // 打开文件
Dim fileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
' // 读取文件的 byte[]
Dim bytes() = New Byte(fileStream.Length) {}
fileStream.Read(bytes, 0, bytes.Length)
fileStream.Close()
' // 把 byte[] 转换成 Stream
' Dim stream = New MemoryStream(bytes)
Dim stream = BytesToStream(bytes)
Return stream
End Function
Public Shared Function BytesToStream(ByVal bytes As Byte()) As Stream
Dim stream = New MemoryStream(bytes)
Return stream
End Function
'//上傳檔案
Public Shared Sub UploadFile(ByVal savefileName As String, ByVal data As Stream)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功
Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim c = New WebClient()
'--------------事件委派動作---------------------------------------
'c.OpenWriteCompleted += (sender, e) =>
' {
' PushData(data, e.Result);
' e.Result.Close();
' data.Close();
' MessageBox.Show("上傳完畢");
' };
' vb對應的寫法如下
AddHandler c.OpenWriteCompleted, Sub(sender, e)
PushData(data, e.Result)
e.Result.Close()
data.Close()
End Sub
'------------/事件委派動作/---------------------------------------
c.OpenWriteAsync(ub.Uri, "POST")
End Sub
Public Shared Sub UploadData(ByVal savefileName As String, ByVal data_str As String)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功
Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim wc = New WebClient()
wc.UploadStringAsync(ub.Uri, data_str)
End Sub
Public Shared Sub PushData(ByVal input As MemoryStream, ByVal output As Stream)
Dim buffer() As Byte = New Byte(4000000) {} ' 4000K =4MB 暫存區
Dim stream As Stream = input
Dim read As Integer = 0
Dim total As Integer = 0
Do '回傳這次讀取了多少個 最多4096 個byte
read = stream.Read(buffer, total, 4096)
output.Write(buffer, total, read)
If (read <> 0) Then
total += read '累計總量
End If
Loop While (read <> 0)
'Dim bytesRead As Integer
'bytesRead = input.Read(buffer, 0, buffer.Length)
'While (input.Read(buffer, 0, buffer.Length)) <> 0
' output.Write(buffer, 0, bytesRead)
'End While
End Sub
End Class
End Namespace
全站熱搜
留言列表