我允许用户上传包含图片路径的excel文件。问题是它抛出了以下错误:
System.IO.DirectoryNotFoundException:找不到部分内容 路径'C:\ Users \ gwphi_000 \ Desktop \ test \ OrderEmail.png'。在 System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess 访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String 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(String path,Boolean checkHost) 在UploadExelFiles.Helpers.GetExcelDataFromFile.ReadExcelFile(String 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);
}
答案 0 :(得分:0)
它无法找到该文件,因为它不存在,或者它没有权限。你说:
在IIS中,我已将应用程序池更改为网络服务和本地 试验机
但是你在服务器上做了同样的事吗?
另外,请记住,在服务器上运行时,它将在服务器上查找文件。路径C:\Users\gwphi_000\Desktop\test\
是否存在于服务器上?
答案 1 :(得分:0)
为了防止其他人需要上传包含图片路径的excel文件,我已经发布了我是如何做到的。
重点是“加布里埃尔·卢西”和“加布里埃尔·鲁奇”。写道,路径在服务器上寻找图像,我会因为厌倦而不去想它而责怪我。
无论如何,现在代码已经重新编写,所以你现在必须上传一个zip文件,在该文件中是excel文件和图像。
如何从excel文件中获取数据的代码如下所示,请注意,这是针对Umbraco的,因此您可能需要修改它以供自己使用。
public static bool ReadExcelFile(string pathToMedia)
{
try
{
string filePath = pathToMedia;
string zipFilePath = HttpContext.Current.Server.MapPath("~/www/UploadExcelFile");
string extractImagesTo = HttpContext.Current.Server.MapPath("~/www/Images/AccessoriesZipImages");
string pathToExcelFileOnServer = string.Empty;
using (ZipArchive archive = ZipFile.OpenRead(filePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||entry.FullName.EndsWith("xls", StringComparison.OrdinalIgnoreCase))
{
if (File.Exists(Path.Combine(zipFilePath, entry.Name)))
{
File.Delete(Path.Combine(zipFilePath, entry.Name));
}
pathToExcelFileOnServer = Path.Combine(zipFilePath, entry.Name);
entry.ExtractToFile(Path.Combine(zipFilePath, entry.Name));
}
}
}
using (ZipArchive archive = ZipFile.OpenRead(filePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (File.Exists(Path.Combine(extractImagesTo, entry.Name)))
{
File.Delete(Path.Combine(extractImagesTo, entry.Name));
}
if (entry.FullName.EndsWith(".png", StringComparison.OrdinalIgnoreCase) || entry.FullName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
{
entry.ExtractToFile(Path.Combine(extractImagesTo, entry.Name));
}
}
}
if (!string.IsNullOrEmpty(pathToExcelFileOnServer))
{
using (var stream = File.Open(pathToExcelFileOnServer, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
List<ShoppingCartViewModel> excelList = new List<ShoppingCartViewModel>();
foreach (DataTable table in result.Tables)
{
foreach (DataRow dr in table.Rows.Cast<DataRow>().Skip(1)) //Skipping header
{
//Cannot seem to use cell names, so use cell location
excelList.Add(new ShoppingCartViewModel(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), Path.Combine(extractImagesTo, dr[3].ToString()), dr[4].ToString(), null, 0.00, null));
}
}
var contentService = ApplicationContext.Current.Services.ContentService;
var allEventsNode = ExcelUploadUmbracoAssignedContentHelper.HomePageContent();
int rootId = allEventsNode.Id;
var itemsForSale = contentService.GetChildren(rootId).Where(x => x.ContentType.Alias == "accessorieItems").ToList();
//Delete all current nodes
foreach (var items in itemsForSale)
{
contentService.Delete(items);
}
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);
}
contentService.RePublishAll();
library.RefreshContent();
Array.ForEach(Directory.GetFiles(extractImagesTo), File.Delete);
//Delete empty folders in media folder
DeleteEmptyFolder.DeleteFolder();
}
}
}
return true;
}
catch (Exception ex)
{
throw new ApplicationException(ex.ToString());
}
}