【发布时间】:2018-06-28 17:47:12
【问题描述】:
我允许用户上传具有图像路径的 excel 文件。问题是它抛出以下错误:
System.IO.DirectoryNotFoundException:找不到一部分 路径“C:\Users\gwphi_000\Desktop\test\OrderEmail.png”。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess 访问、Int32 权限、布尔用户权限、FileShare 共享、Int32 bufferSize,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs,字符串 msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问、FileShare 共享、Int32 bufferSize、FileOptions 选项、字符串 msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.File.InternalReadAllBytes(字符串路径,布尔检查主机) 在 UploadExelFiles.Helpers.GetExcelDataFromFile.ReadExcelFile(字符串 pathToMedia)
在 IIS 中,我已将应用程序池更改为网络服务,并且在我的本地测试机器上可以正常工作,但是在远程服务器上它会引发错误。
我的代码是
foreach (var item in excelList)
{
string fileName = Guid.NewGuid().ToString();
var content = contentService.CreateContent(item.Product, allEventsNode.Id, "accessorieItems");
IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
var newImage = mediaService.CreateMedia($"{fileName}.jpeg", -1, "Image");
byte[] buffer = File.ReadAllBytes(Path.GetFullPath(item.ProductUrl));
MemoryStream strm = new MemoryStream(buffer);
newImage.SetValue("umbracoFile", $"{fileName}.jpeg", strm);
mediaService.Save(newImage);
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var image = umbracoHelper.Media(newImage.Id).Url;
string imagePath = image.ToString();
content.SetValue("productId", item.ProductId);
content.SetValue("productTitle", item.Product);
content.SetValue("productPrice", item.Price);
content.SetValue("productImage", imagePath);
content.SetValue("productDescription", item.ProductDescription);
contentService.SaveAndPublishWithStatus(content);
}
【问题讨论】:
-
能否提供一个excel文件数据的例子?
-
您正在尝试从服务器访问用户的文件系统,这就是您可能遇到此错误的原因。在本地,它允许您使用它的本地服务器,但远程机器不能这样做。