private Task<HttpResponseMessage> Post(string rootPath, string folder,bool newFileName)
{
var PATH = HttpHelper.AbsolutePath(rootPath); if (Request.Content.IsMimeMultipartContent())
{
string uploadPath = string.IsNullOrEmpty(folder) ? PATH : Path.Combine(PATH, folder);
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
} var streamProvider = new CustomMultipartFormDataStreamProvider(uploadPath, newFileName);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<HttpResponseMessage>(t =>
{ if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
} var fileInfo = streamProvider.FileData.Select(i =>
{
var oldFileName = i.Headers.ContentDisposition.FileName;
oldFileName = oldFileName.Replace("\"", string.Empty); var info = new FileInfo(i.LocalFileName);
string fileName = info.Name; //后缀名
string extenstion = string.Empty;
if (fileName.IndexOf(".") >= 0) //含有后缀名
extenstion = fileName.Substring(fileName.LastIndexOf(".") + 1); long size = info.Length / 1024; //返回的路径,不再添加主目录 ---
//string newFilePath = string.IsNullOrEmpty(folder) ? rootPath + "/" + fileName : rootPath + "/" + folder + "/" + fileName;
string newFilePath = string.IsNullOrEmpty(folder) ? fileName : folder + "/" + fileName; return new FileDesc(oldFileName, newFilePath, size, extenstion);
//return new FileDesc(info.Name, rootUrl + "/" + folderName + "/" + info.Name, info.Length / 1024);
});
return Request.CreateResponse(HttpStatusCode.OK, fileInfo);
}); return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
} public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
private bool _newFileName = false; public CustomMultipartFormDataStreamProvider(string path)
: this(path, true)
{ } public CustomMultipartFormDataStreamProvider(string path, bool newFileName)
: base(path)
{
_newFileName = newFileName;
} public override string GetLocalFileName(HttpContentHeaders headers)
{
var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
name = name.Replace("\"", string.Empty); if (_newFileName)
{
string newFileName = GetNewFileName(name); return newFileName;
//this is here because Chrome submits files in quotation s
//which get treated as part of the filename and get escaped
} //返回原有名称
return name;
} /// <summary>
/// 获取新的名称 比如:aa.jpg转化为aa(20090504).jpg
/// </summary>
/// <param name=”fileName”>文件名称[aa.jpg]</param>
/// <returns>新的文件名称</returns>
public static string GetNewFileName(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return string.Empty; string newFileName = string.Empty;
if (fileName.IndexOf(".")>=0) //含有后缀名
{
//文件后缀名
string extenstion = fileName.Substring(fileName.LastIndexOf(".") + 1);
string name = fileName.Substring(0, fileName.LastIndexOf(".")) + "(" + DateTime.Now.ToFileTime() + ")";
newFileName = name + "." + extenstion;
}
else
{
string name = fileName + "(" + DateTime.Now.ToFileTime() + ")";
newFileName = name;
}
return newFileName;
}
}
{
var PATH = HttpHelper.AbsolutePath(rootPath); if (Request.Content.IsMimeMultipartContent())
{
string uploadPath = string.IsNullOrEmpty(folder) ? PATH : Path.Combine(PATH, folder);
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
} var streamProvider = new CustomMultipartFormDataStreamProvider(uploadPath, newFileName);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<HttpResponseMessage>(t =>
{ if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
} var fileInfo = streamProvider.FileData.Select(i =>
{
var oldFileName = i.Headers.ContentDisposition.FileName;
oldFileName = oldFileName.Replace("\"", string.Empty); var info = new FileInfo(i.LocalFileName);
string fileName = info.Name; //后缀名
string extenstion = string.Empty;
if (fileName.IndexOf(".") >= 0) //含有后缀名
extenstion = fileName.Substring(fileName.LastIndexOf(".") + 1); long size = info.Length / 1024; //返回的路径,不再添加主目录 ---
//string newFilePath = string.IsNullOrEmpty(folder) ? rootPath + "/" + fileName : rootPath + "/" + folder + "/" + fileName;
string newFilePath = string.IsNullOrEmpty(folder) ? fileName : folder + "/" + fileName; return new FileDesc(oldFileName, newFilePath, size, extenstion);
//return new FileDesc(info.Name, rootUrl + "/" + folderName + "/" + info.Name, info.Length / 1024);
});
return Request.CreateResponse(HttpStatusCode.OK, fileInfo);
}); return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
} public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
private bool _newFileName = false; public CustomMultipartFormDataStreamProvider(string path)
: this(path, true)
{ } public CustomMultipartFormDataStreamProvider(string path, bool newFileName)
: base(path)
{
_newFileName = newFileName;
} public override string GetLocalFileName(HttpContentHeaders headers)
{
var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
name = name.Replace("\"", string.Empty); if (_newFileName)
{
string newFileName = GetNewFileName(name); return newFileName;
//this is here because Chrome submits files in quotation s
//which get treated as part of the filename and get escaped
} //返回原有名称
return name;
} /// <summary>
/// 获取新的名称 比如:aa.jpg转化为aa(20090504).jpg
/// </summary>
/// <param name=”fileName”>文件名称[aa.jpg]</param>
/// <returns>新的文件名称</returns>
public static string GetNewFileName(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return string.Empty; string newFileName = string.Empty;
if (fileName.IndexOf(".")>=0) //含有后缀名
{
//文件后缀名
string extenstion = fileName.Substring(fileName.LastIndexOf(".") + 1);
string name = fileName.Substring(0, fileName.LastIndexOf(".")) + "(" + DateTime.Now.ToFileTime() + ")";
newFileName = name + "." + extenstion;
}
else
{
string name = fileName + "(" + DateTime.Now.ToFileTime() + ")";
newFileName = name;
}
return newFileName;
}
}
本地文件 map.jpg传到服务器apitest.myserver.com网站下的uploadfile文件夹下,这里的filepath是写map.jpg 还是 【本地绝对路径+map.jpg】我测试哪个都不对啊,按理说默认是使用post方法,但是却会提示【远程服务器返回错误: (405) 不允许的方法】
写一个服务器端接受页面(推荐ashx),并且调试。贴出你的调试画面。
写一个服务器端接受页面(推荐ashx),并且调试。贴出你的调试画面。不是不需要接受页面吗
直接使用put还不行
我已经开启了webdav服务模块,在IIS7的处理程序映射中搞不清 该设置哪个请求限制 允许put操作。还有没有其他限制?
写一个服务器端接受页面(推荐ashx),并且调试。贴出你的调试画面。不是不需要接受页面吗你写一个6、7行代码的接受页面,不就就能方便自己调试了嘛。