Webclient.UploadFile引发异常 [英] Webclient.UploadFile throws an exception
问题描述
问题: Webclient.UploadFile方法使我异常远程服务器返回未找到错误404"
场景:我的服务器上有一个虚拟目录(http://xx.xx.xx.xx/Uploadfile)
现在我想将文件从本地计算机上传到服务器.我使用了以下代码
wcUpload = 新 WebClient();
wcUpload.Credentials = CredentialCache.DefaultCredentials;
wcUpload.UploadFile(" C:\test.pdf" 解决方案
Shreya,
它尝试查找不存在的文件http://xx.xx.xx.xx/Uploadfile/test.pdf
.您需要在此处创建一个接受文件并存储在目录中的页面.wcUpload.UploadFile(" C:\ test.pdf )
请参阅此MSDN示例 http://msdn.microsoft.com/en-us/library/36s52zhs.aspx [ ^ ]
webClient.DownloadFile()将允许从源直接下载到目标,但是UploadFile()方法不是这种情况.
应该存在一些接收者页面来获取文件,然后该页面会将您的文件发布到目的地.
正如prerak在服务器上引用的创建upload.aspx
"页面所引用的那样,将以下代码放入其中,并从客户端调用它以上传文件
foreach (字符串 f 在 Request.Files.AllKeys中){ HttpPostedFile文件= Request.Files [f]; file.SaveAs(" + file.FileName);
Problem : Webclient.UploadFile method gives me exception "remote server return an error 404 not found"
Scenario : I have a virtual directory on my server(http://xx.xx.xx.xx/Uploadfile)
now i want to upload a file from my local machine to server. i used following code
wcUpload = new WebClient();
wcUpload.Credentials = CredentialCache.DefaultCredentials;
wcUpload.UploadFile("http://xx.xx.xx.xx/Uploadfile/test.pdf, @"C:\test.pdf"); //this line throws exception
i have test.pdf exist on my C:\
i have checked for access permission to virtual directory, it is fine.
what should be the possible solution.
any help ?
Shreya,
It tries to look for the filehttp://xx.xx.xx.xx/Uploadfile/test.pdf
which doesn''t exist. You need to create a page there which accepts the file and store at directory.wcUpload.UploadFile("http://xx.xx.xx.xx/upload.aspx, @"C:\test.pdf")
See this MSDN example http://msdn.microsoft.com/en-us/library/36s52zhs.aspx[^]
webClient.DownloadFile() will allow direct download from source to destination, but this is not the case with UploadFile() method.
There should exist some reciever page to get the file and that page will then POST your file to destnation.
As prerak quoted Createupload.aspx
page at server put following code in it and call it from your client to upload file
foreach(string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files[f]; file.SaveAs("Path to save file\\" + file.FileName);
这篇关于Webclient.UploadFile引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!