求大神指点 vb.net 怎么发送大文件 我用 UdpClient 的Send只能发送5000字节以下的
求大神指点vb.net怎么发送大文件我用UdpClient的Send只能发送5000字节以下的ImportsSystem.Threading.ThreadImportsS...
求大神指点 vb.net 怎么发送大文件 我用 UdpClient 的Send只能发送5000字节以下的
Imports System.Threading.Thread
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
Dim s As New UdpClient '这里注意发送文件不再使用Socket,而是用UdpClient
'====================================
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtFile.Text = "c:\a12.bmp"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'发送文件
If txtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
Try
Dim IPEndPoin As New IPEndPoint(Net.IPAddress.Parse(Trim(txtIP.Text)), CInt(txtPort.Text))
Dim fs As New FileStream(txtFile.Text, FileMode.OpenOrCreate, FileAccess.Read) ''要传输的文件
Dim File(fs.Length - 1) As Byte '定义File()数组
Dim strread As New BinaryReader(fs)
strread.Read(File, 0, File.Length) '读取要传输的文件数据
MsgBox(fs.Length)
s.Send(File, File.Length, IPEndPoin) '发送文件
lblLen.Text = "已发送字节数:" & fs.Length()
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class 展开
Imports System.Threading.Thread
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
Dim s As New UdpClient '这里注意发送文件不再使用Socket,而是用UdpClient
'====================================
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtFile.Text = "c:\a12.bmp"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'发送文件
If txtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
Try
Dim IPEndPoin As New IPEndPoint(Net.IPAddress.Parse(Trim(txtIP.Text)), CInt(txtPort.Text))
Dim fs As New FileStream(txtFile.Text, FileMode.OpenOrCreate, FileAccess.Read) ''要传输的文件
Dim File(fs.Length - 1) As Byte '定义File()数组
Dim strread As New BinaryReader(fs)
strread.Read(File, 0, File.Length) '读取要传输的文件数据
MsgBox(fs.Length)
s.Send(File, File.Length, IPEndPoin) '发送文件
lblLen.Text = "已发送字节数:" & fs.Length()
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class 展开
2个回答
展开全部
下载,直接通过url读取文件,然后Response.OutputStream.Write()数据
下面提供个下载的静态方法,是C#的,供参考:
/// <summary>
/// 下载文件
/// </summary>
/// <param name="fileName">下载的文件名称(包括扩展名)</param>
/// <param name="filePath">下载文件的绝对路径</param>
public static void DownFile(string fileName, string filePath)
{
//打开要下载的文件,并把该文件存放在FileStream中
System.IO.FileStream Reader = System.IO.File.OpenRead(filePath);
//文件传送的剩余字节数:初始值为文件的总大小
long Length = Reader.Length;
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName));
HttpContext.Current.Response.AddHeader("Content-Length", Length.ToString());
byte[] Buffer = new Byte[10000];//存放欲发送数据的缓冲区
int ByteToRead; //每次实际读取的字节数
while (Length > 0)
{
//剩余字节数不为零,继续传送
if (HttpContext.Current.Response.IsClientConnected)
{
//客户端浏览器还打开着,继续传送
ByteToRead = Reader.Read(Buffer, 0, 10000); //往缓冲区读入数据
HttpContext.Current.Response.OutputStream.Write(Buffer, 0, ByteToRead);
//把缓冲区的数据写入客户端浏览器
HttpContext.Current.Response.Flush(); //立即写入客户端
Length -= ByteToRead;//剩余字节数减少 }
else
{
//客户端浏览器已经断开,阻止继续循环
Length = -1;
}
} //关闭该文件
Reader.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询