gpt4 book ai didi

c# - ASP.NET 将 .doc 转换为 .pdf 相对路径映射失败

转载 作者:行者123 更新时间:2023-12-04 07:14:08 26 4
gpt4 key购买 nike

每当我上传文件时,我想让它自动转换为
.pdf(我正在使用 NuGet 这样做)。问题是上传方案是使用
相对路径。我现在知道在这个括号中放入什么:
var wordDocument = appWord.Documents.Open(uploadedFile);
这给出了一个空异常;说没有找到文件。
请注意,没有转换部分的上传代码可以正常工作。
我应该用什么来替换 uploadFile 才能工作?我会离开我的
下面的真实路径映射代码,所以你可以看到我所做的一切
为了更好地帮助您(以及我)处理应该放入的内容
那个括号是为了工作?非常感谢!

public IActionResult Index1()
{
// Get files from the server
var model = new FilesViewModel();
foreach (var item in Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "upload")))
{
model.Files.Add(
new FileDetails { Name = System.IO.Path.GetFileName(item), Path = item });
}
return View(model);
}

[HttpPost]
public IActionResult Index1(IFormFile[] files)
{
// Iterate each files
foreach (var file in files)
{
// Get the file name from the browser
var fileName = System.IO.Path.GetFileName(file.FileName);

// Get file path to be uploaded
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "upload", fileName);



// Check If file with same name exists and delete it
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}

// Create a new local file and copy contents of uploaded file
using (var localFile = System.IO.File.OpenWrite(filePath))
using (var uploadedFile = file.OpenReadStream())
{
var appWord = new Application();
if (appWord.Documents != null)
{
//yourDoc is your word document
var wordDocument = appWord.Documents.Open(file) ;
string pdfDocName = "pdfDocument.pdf";
if (wordDocument != null)
{
wordDocument.ExportAsFixedFormat(pdfDocName,
WdExportFormat.wdExportFormatPDF);
wordDocument.Close();
}
appWord.Quit();
}


uploadedFile.CopyTo(localFile);





}

}
ViewBag.Message = "Files are successfully uploaded";

// Get files from the server
var model = new FilesViewModel();
foreach (var item in Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "upload")))
{
model.Files.Add(
new FileDetails { Name = System.IO.Path.GetFileName(item), Path = item });

}
return View(model);
}

public async Task<IActionResult> Download(string filename)
{
if (filename == null)
return Content("filename is not availble");

var path = Path.Combine(Directory.GetCurrentDirectory(), "upload", filename);

var memory = new MemoryStream();
using (var stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, GetContentType(path), Path.GetFileName(path));
}
private string GetContentType(string path)
{
var types = GetMimeTypes();
var ext = Path.GetExtension(path).ToLowerInvariant();
return types[ext];
}

private Dictionary<string, string> GetMimeTypes()
{
return new Dictionary<string, string>
{
{".txt", "text/plain"},
{".pdf", "application/pdf"},
{".doc", "application/vnd.ms-word"},
{".docx", "application/vnd.ms-word"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".png", "image/png"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".gif", "image/gif"},
{".csv", "text/csv"}
};
}```

最佳答案

//Get the path of existing Word document
string fullpath = @"...\..\DocToPDF.docx";

//Loads an existing Word document
WordDocument wordDocument = new WordDocument(fullpath, FormatType.Docx);

//Creates an instance of the DocToPDFConverter
DocToPDFConverter converter = new DocToPDFConverter();

//Converts Word document into PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);

//Releases all resources used by DocToPDFConverter
converter.Dispose();

//Closes the instance of document objects
wordDocument.Close();

//Saves the PDF file
pdfDocument.Save("DocToPDF.pdf");

//Closes the instance of document objects
pdfDocument.Close(true);```

this is the NuGet documentation they had on the site, I have got problems at the 2nd row : on specifying the full path

关于c# - ASP.NET 将 .doc 转换为 .pdf 相对路径映射失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68888648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com