我正在寻找编写C#客户端应用程序。在这个应用程序中,我需要将文件上传到服务器并让服务器响应一个字符串(上传文件的唯一标识符。)
我熟悉并且熟悉C#和ASP.Net MVC。我可以使用这些技术以相对安全的方式执行此操作,还是需要使用某种WCF Web服务thingie-mah-jig? (我注意到'相对安全',因为没有上传的内容是保密的,绝对不是关键任务的)
在任何一种情况下,关于该主题的任何资源(即使它恰好在VB.Net中)都会很棒。
非常感谢!
编辑:发布一些代码。 我有一个简单的网站设置,只有一个事件接受带文件的POST:
[HttpPost]
public string Upload(HttpPostedFileBase file)
{
string sPath = "";
if (file != null && file.ContentLength > 0)
{
try
{
string sFileName = "test.txt";
// Save file
sPath = Path.Combine(Server.MapPath("~/App_Data/uploads"), sFileName).ToString();
file.SaveAs(sPath);
// Return val
return sPath;
}
catch
{
return "Error on Save .. tried to save to\n" + sPath;
}
}
return "no file";
}
在客户端,C#应用程序执行此操作:
using (WebClient wc = new WebClient())
{
try
{
byte[] bResponse = wc.UploadFile("http://127.0.0.1/ISSHost/file/upload", "POST", "C:\\test.txt");
string sResponse = System.Text.Encoding.ASCII.GetString(bResponse);
MessageBox.Show("\nResponse received: " + sResponse);
}
catch(Exception exception)
{
string sError = exception.ToString();
if (exception.InnerException != null)
sError += "\n" + exception.InnerException.ToString();
MessageBox.Show("Error!\n" + sError);
}
}
this.Dispose();
}
有效。我想知道的是,如果我以完全错误的方式解决这个问题,或者我可能会暴露出什么样的安全漏洞......等等我这样做会产生什么影响?还有更好的方法吗?
更重要的是,如何防止最终用户尝试导航到上传页面(在这种情况下 - http://127.0.0.1/ISSHost/file/upload
实际上没有“GET”部分到页面,所以它只是404。)
再次感谢!
答案 0 :(得分:0)
请尝试在客户端使用github的fileuploader.js并返回字符串,您的服务器端代码将是这样的..
[HttpPost]
public ActionResult Upload(string fileName)
{
------------
//your code ...
return Json(new{resultString="Uploaded Successfully."});
}