日期:2014-05-17  浏览次数:20543 次

文件上传时出现“未能找到文件”的错误,请帮忙排错或给出能用put方法上传文件的例子,多谢!
文件上传代码
C# code
        string fileName = fuUploadFile.PostedFile.FileName;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);  //加入调试代码发现问题出在这一行
        WebClient myWebClient = new WebClient();
        string uriString = Server.MapPath(@"./") + "uploadFiles" + System.IO.Path.GetFileName(fileName);
        BinaryReader br = new BinaryReader(fs);
        Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));
        Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
        if (postStream.CanWrite)
        {
            postStream.Write(postArray, 0, postArray.Length);
        }
        postStream.Close();
        fs.Close();


错误堆栈
C# code
[FileNotFoundException: 未能找到文件“E:\timer.txt”。]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +328
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) +57
   _Default.btnUploadFile_Click(Object sender, EventArgs e) +56
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746


E:\timer.txt 是客户端的文件
查了很多关于这一错误的帖子,但都是说服务器端路径或权限的问题,而看代码出错的行,应该是读取客户端文件时出错,还没有涉及服务器端代码的执行

我现在有一个用put方法上传文件的需求,解决这个问题,或者给出能用put方法上传文件的例子都行,多谢大家



------解决方案--------------------
String p = Server.MapPath("~/" + Path.GetFileName(fuUploadFile.PostedFile.FileName));
fuUploadFile.PostedFile.SaveAs(p);

FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read);