silverlight 如何写入指定目录下的文件
展开全部
不明白你是想怎么写,两种情况:
1.已有文件源或者stream,采用楼上方法试试;
2.比较好的方法:出于安全性考虑,客户端是不能直接读取或者写文件的,你可以通过webservice对指定文件流写入指定文件,贴一个webservice类似的方法:
首先客户端获取打开文件(VB代码):
Dim dialog = New OpenFileDialog() With { _
.Multiselect = False, _
.Filter = "All Files (*.*)|*.*"
}
If dialog.ShowDialog() = True Then
If dialog.Files IsNot Nothing Then
Dim f As FileInfo = dialog.File
_fileSteam = f.OpenRead
filename = f.ToString
yichang.AttachFileName = filename
Me.txtAttachFileName.Text = f.ToString
Else
End If
End If
服务端
<WebMethod()> _
Public Function UpLoadAttachFile(ByVal FileByte As Byte(), ByVal offSet As Long, ByVal AttachFileName As String, ByVal AttachReFileName As String) As String
Dim _fileOperatorPath As String = Server.MapPath("FilesPath/")
Dim _attachfilename As String = ""
If AttachReFileName <> "" Then
_attachfilename = AttachReFileName
Else
'获取文件后缀
Dim _filter As String = AttachFileName.ToString.Substring(AttachFileName.ToString.LastIndexOf("."))
'重命名文件
_attachfilename = Convert.ToDateTime(Now.ToString.Replace("/", "-")).ToString("yyyyMMddHHmmss") & _filter
End If
Dim _AttachFileSavePath As String = _fileOperatorPath & _attachfilename
Try
Dim buffer As Byte() = FileByte
If buffer IsNot Nothing Then
'读写文件的文件流,支持同步读写也支持异步读写
Dim filesStream As New FileStream(_AttachFileSavePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
filesStream.Seek(offSet, SeekOrigin.Begin)
filesStream.Write(buffer, 0, buffer.Length)
filesStream.Flush()
filesStream.Close()
filesStream.Dispose()
Else
_attachfilename = ""
End If
Catch ex As Exception
_attachfilename = ""
End Try
Return _attachfilename
End Function
1.已有文件源或者stream,采用楼上方法试试;
2.比较好的方法:出于安全性考虑,客户端是不能直接读取或者写文件的,你可以通过webservice对指定文件流写入指定文件,贴一个webservice类似的方法:
首先客户端获取打开文件(VB代码):
Dim dialog = New OpenFileDialog() With { _
.Multiselect = False, _
.Filter = "All Files (*.*)|*.*"
}
If dialog.ShowDialog() = True Then
If dialog.Files IsNot Nothing Then
Dim f As FileInfo = dialog.File
_fileSteam = f.OpenRead
filename = f.ToString
yichang.AttachFileName = filename
Me.txtAttachFileName.Text = f.ToString
Else
End If
End If
服务端
<WebMethod()> _
Public Function UpLoadAttachFile(ByVal FileByte As Byte(), ByVal offSet As Long, ByVal AttachFileName As String, ByVal AttachReFileName As String) As String
Dim _fileOperatorPath As String = Server.MapPath("FilesPath/")
Dim _attachfilename As String = ""
If AttachReFileName <> "" Then
_attachfilename = AttachReFileName
Else
'获取文件后缀
Dim _filter As String = AttachFileName.ToString.Substring(AttachFileName.ToString.LastIndexOf("."))
'重命名文件
_attachfilename = Convert.ToDateTime(Now.ToString.Replace("/", "-")).ToString("yyyyMMddHHmmss") & _filter
End If
Dim _AttachFileSavePath As String = _fileOperatorPath & _attachfilename
Try
Dim buffer As Byte() = FileByte
If buffer IsNot Nothing Then
'读写文件的文件流,支持同步读写也支持异步读写
Dim filesStream As New FileStream(_AttachFileSavePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
filesStream.Seek(offSet, SeekOrigin.Begin)
filesStream.Write(buffer, 0, buffer.Length)
filesStream.Flush()
filesStream.Close()
filesStream.Dispose()
Else
_attachfilename = ""
End If
Catch ex As Exception
_attachfilename = ""
End Try
Return _attachfilename
End Function
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.IO.File.Create
或
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == true)
{
Stream stream = null;
stream = sfd.OpenFile();
BinaryWriter bw = new BinaryWriter(stream);
stream.Read(Data, 0, Data.Length);
bw.Write(Data);
bw.Close();
}
或
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == true)
{
Stream stream = null;
stream = sfd.OpenFile();
BinaryWriter bw = new BinaryWriter(stream);
stream.Read(Data, 0, Data.Length);
bw.Write(Data);
bw.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询