更改IIS Express查找文件的目录(C#、jquery$.ajax和KendoUI)
我想找到一种方法,这样我就可以选择iisexpress在哪个目录下查找文件,这些文件稍后将通过web服务通过$.ajax调用进行处理。 比如说,更改IIS Express查找文件的目录(C#、jquery$.ajax和KendoUI),c#,jquery,ajax,web-services,kendo-ui,C#,Jquery,Ajax,Web Services,Kendo Ui,我想找到一种方法,这样我就可以选择iisexpress在哪个目录下查找文件,这些文件稍后将通过web服务通过$.ajax调用进行处理。 比如说, $.ajax({ url: destination.url, data: {file: myfile}, type: "post", success: function(json) { [...] }, error:function (xhr, ajaxOptions, thrownError) { [...
$.ajax({
url: destination.url,
data: {file: myfile},
type: "post",
success: function(json) {
[...]
},
error:function (xhr, ajaxOptions, thrownError) {
[...]
}
});
myfile变量的内容将作为文件参数传递给web服务,但始终相对于IIS Express启动路径,在本例中是C:\Program Files\IIS Express,更准确地说是C:\Program Files\IIS Express\myfile。
可以说,我想设置某种基本URL,以便IIS Express在我的应用程序路径中的文件夹中查找文件,例如C:\Users\me\myapp\my\U files。
只要有可能,我希望在每个应用程序的基础上执行此操作,并且我不希望只是按照建议硬编码路径(因为我正在使用IIS Express的开发机器上工作,但应用程序将发布到运行IIS的服务器)。
任何帮助都将不胜感激。提前感谢。您必须选择站点后面的某个位置来存放临时文件。每个应用程序的位置都应该更改。我将在下面的示例中使用temp:
public string GetPhysicalTempFolder()
{
return AppDomain.CurrentDomain.BaseDirectory + @"Temp\";
}
private string GetVirtualTempFolder()
{
//Returns ~/Temp/
if (Url != null)
return System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~/Temp/");
else
return VirtualPathUtility.ToAbsolute("~/Temp/");
}
在上载控制器中,您需要将文件保存到指定位置
//---------------------------------------------------------------------------------------------------
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files)
{
try
{
// The Name of the Upload component is "files"
if (files == null || files.Count() == 0)
throw new ArgumentException("No files defined");
HttpPostedFileBase file = files.ToArray()[0];
if (file.ContentLength > 10485760)
throw new ArgumentException("File cannot exceed 10MB");
file.InputStream.Position = 0;
Byte[] destination = new Byte[file.ContentLength];
file.InputStream.Read(destination, 0, file.ContentLength);
//do something with destination here
}
catch (Exception e)
{
model.UploadError = e.Message;
}
return Json(model, JsonRequestBehavior.AllowGet);
}
//---------------------------------------------------------------------------------------------------
公共操作结果上载文件(IEnumerable文件)
{
尝试
{
//上载组件的名称为“文件”
如果(files==null | | files.Count()==0)
抛出新ArgumentException(“未定义文件”);
HttpPostedFileBase file=files.ToArray()[0];
如果(file.ContentLength>10485760)
抛出新ArgumentException(“文件不能超过10MB”);
file.InputStream.Position=0;
Byte[]destination=新字节[file.ContentLength];
file.InputStream.Read(目标,0,file.ContentLength);
//在这里做点什么
}
捕获(例外e)
{
model.UploadError=e.消息;
}
返回Json(model,JsonRequestBehavior.AllowGet);
}