asp.net mvc web api保存图片到项目文件夹报错

殷小强 2016-05-26 12:16:30
想要实现的功能是:根据图片的url生成一张图片,然后把图片保存到项目中某个文件夹里。

[ResponseType(typeof(PictureModel))]
public IHttpActionResult PostPictureModel(PictureModel pictureModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//上传图片到文件夹
string basePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//GetImg(pictureModel.PicUrl);
DownloadPicture(pictureModel.PicUrl, basePath + "\\Upload\\Pics", 18000);
db.PictureModels.Add(pictureModel);
db.SaveChanges();

return CreatedAtRoute("DefaultApi", new { id = pictureModel.ID }, pictureModel);
}private static bool DownloadPicture(string picUrl, string savePath, int timeOut)
{
bool value = false;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUrl);
if (timeOut != -1) request.Timeout = timeOut;
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
value = SaveBinaryFile(response, savePath);
}
finally
{
if (stream != null) stream.Close();
if (response != null) response.Close();
}
return value;
}
private static bool SaveBinaryFile(WebResponse response, string savePath)
{
bool value = false;
byte[] buffer = new byte[1024];
Stream outStream = null;
Stream inStream = null;
try
{
if (File.Exists(savePath)) File.Delete(savePath);
outStream = System.IO.File.Create(savePath);
inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0) outStream.Write(buffer, 0, l);
} while (l > 0);
value = true;
}
finally
{
if (outStream != null) outStream.Close();
if (inStream != null) inStream.Close();
}
return value;
}


错误:
{"readyState":4,"responseText":"{\"Message\":\"出现错误。\",\"ExceptionMessage\":\"对路径“C:\\\\_github\\\\UMAPP\\\\UMAPP\\\\Upload\\\\Pics”的访问被拒绝。\",\"ExceptionType\":\"System.UnauthorizedAccessException\",\"StackTrace\":\" 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\\r\\n 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\\r\\n 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)\\r\\n 在 System.IO.File.Create(String path)\\r\\n 在 UMAPP.Controllers.CollectController.SaveBinaryFile(WebResponse response, String savePath) 位置 C:\\\\_github\\\\UMAPP\\\\UMAPP\\\\Controllers\\\\CollectController.cs:行号 193\\r\\n 在 UMAPP.Controllers.CollectController.DownloadPicture(String picUrl, String savePath, Int32 timeOut) 位置 C:\\\\_github\\\\UMAPP\\\\UMAPP\\\\Controllers\\\\CollectController.cs:行号 175\\r\\n 在 UMAPP.Controllers.CollectController.PostPictureModel(PictureModel pictureModel) 位置 C:\\\\_github\\\\UMAPP\\\\UMAPP\\\\Controllers\\\\CollectController.cs:行号 86\\r\\n 在 lambda_method(Closure , Object , Object[] )\\r\\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\\r\\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\\r\\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\\r\\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\\r\\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\\r\\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\\r\\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()\"}","responseJSON":{"Message":"出现错误。","ExceptionMessage":"对路径“C:\\_github\\UMAPP\\UMAPP\\Upload\\Pics”的访问被拒绝。","ExceptionType":"System.UnauthorizedAccessException","StackTrace":" 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)\r\n 在 System.IO.File.Create(String path)\r\n 在 UMAPP.Controllers.CollectController.SaveBinaryFile(WebResponse response, String savePath) 位置 C:\\_github\\UMAPP\\UMAPP\\Controllers\\CollectController.cs:行号 193\r\n 在 UMAPP.Controllers.CollectController.DownloadPicture(String picUrl, String savePath, Int32 timeOut) 位置 C:\\_github\\UMAPP\\UMAPP\\Controllers\\CollectController.cs:行号 175\r\n 在 UMAPP.Controllers.CollectController.PostPictureModel(PictureModel pictureModel) 位置 C:\\_github\\UMAPP\\UMAPP\\Controllers\\CollectController.cs:行号 86\r\n 在 lambda_method(Closure , Object , Object[] )\r\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"},"status":500,"statusText":"Internal Server Error"}
...全文
213 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
殷小强 2016-05-26
  • 打赏
  • 举报
回复
在网上找了资料,设置了文件夹的访问权限,添加了everyuser net service 等用户都不管用
殷小强 2016-05-26
  • 打赏
  • 举报
回复
引用 2 楼 yahle 的回复:
文件名去哪里了,我只看到目录,没看到具体要保存到的文件名
谢谢,犯了低级错误。确实是这个问题。目录权限设置了,缺了保存路径的文件名。
yahle 2016-05-26
  • 打赏
  • 举报
回复
文件名去哪里了,我只看到目录,没看到具体要保存到的文件名

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧