我想使用c#函数将文件上传到服务器。我在WebMatrix中为ASP.NET(.CSHTML)WebPages使用了这个函数。在C#函数中使用HttpContext
public static void Upload(string fileSavePath, HttpContext context)
{
var fileCount = context.Request.Files.Count;
if (fileCount > 0)
{
for (int i = 0; i < fileCount; i++)
{
var file = context.Request.Files[i];
if (file.ContentLength > 0)
{
var fileUpload = new WebImage(file.InputStream);
var newFileTitle = RandomString.GenRandomString(10, 4);
var fileExtention = Path.GetExtension(file.FileName).Trim();
var newFileName = newFileTitle + fileExtention;
var fileSaveLocation = HttpContext.Current.Server.MapPath(fileSavePath);
if(!Directory.Exists(fileSaveLocation))
{
Directory.CreateDirectory(fileSaveLocation);
}
fileUpload.Save(fileSaveLocation+"/"+newFileName);
}
}
}
}
我的问题是如何在上传表单文件中调用这个函数?
Chec this:http://stackoverflow.com/questions/25125127/asp-net-mvc-4-c-sharp-httppostedfilebase-how-do-i-store-file – 2014-12-07 08:22:29
不适合我,因为我不要使用MVC – Apurva 2014-12-07 10:00:52