FileStream方法支持URI格式吗?100分

zijie 2004-01-12 07:12:00
System.IO.Stream iStream = null;
string filepath = "http://localhost/3.mpeg";
string filename = System.IO.Path.GetFileName(filepath);
try
{
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);
……

请问为什么在运行时总要报“不支持 URI 格式”,这个错误!难道FileStream不支持URI格式吗?我应该怎么做……

...全文
690 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
速马 2004-01-12
  • 打赏
  • 举报
回复
当然不行,FileStream只提供本地(网上邻居也算)文件流
你可以使用HttpWebRequest和HttpWebResponse类

// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
zhongkeruanjian 2004-01-12
  • 打赏
  • 举报
回复
不好意思,不支持,
你先把你的URL转成物理路
比如string filepath = SERVER。PYHICALPATH(“。/3。MEPG”)
zijie 2004-01-12
  • 打赏
  • 举报
回复
没人肯帮忙吗……………………郁闷%……※×()!◎
<script type="text/C#" runat="server"> BinaryReader ms; UploadInfo uploadInfo = null; protected void Page_Load(object sender, EventArgs args) { if (this.IsPostBack) { uploadInfo = this.Session["UploadInfo"] as UploadInfo; if (uploadInfo == null) { // 让父页面知道无法处理上传 const string js = "window.parent.onComplete('error', '无法上传文件。请刷新页面,然后再试一次);"; ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", js, true); } else { // 让服务端知道我们还没有准备好.. uploadInfo.IsReady = false; try { // 读取要上传的文件 FileStream fs = new FileStream(this.fileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read); ms = new BinaryReader(fs); string newFile = DateTime.Now.Ticks.ToString(); string exit = this.fileUpload.FileName.Substring(this.fileUpload.FileName.IndexOf('.')); newFile = newFile + exit; UriBuilder url = new UriBuilder("http://192.168.25.27:8056/UploadFileHander.ashx");//上传路径 url.Query = string.Format("filename={0}", newFile);//上传url参数 uploadInfo.ContentLength = this.fileUpload.PostedFile.ContentLength; uploadInfo.FileName = newFile; uploadInfo.UploadedLength = 0; //文件存在 初始化... uploadInfo.IsReady = true; WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);//委托异步上传事件 wc.OpenWriteAsync(url.Uri);//开始异步上传 const string js = "window.parent.onComplete('success', '{0} 已成功上传,重命名为:{1}:文件大小:{2}');"; ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", string.Format(js, Path.GetFileName(this.fileUpload.FileName), DateTime.Now.ToString("yyyy-MM") + "/" + DateTime.Now.Day + "/" + newFile, uploadInfo.ContentLength), true); } catch (Exception ex) { Response.Write(ex.Message); } } } } protected void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { int bufSize = 10; int byteGet = 0; byte[] buf = new byte[bufSize]; while ((byteGet = ms.Read(buf, 0, bufSize)) > 0)//循环读取,上传 { e.Result.Write(buf, 0, byteGet);//注意这里 uploadInfo.UploadedLength += byteGet; } // 让父页面知道已经处理上传完毕 e.Result.Close();//关闭 ms.Close(); }

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧