简体   繁体   English

将文件从本地计算机上传到服务器 c#

[英]Upload File from local computer to server c#

Is there any way to upload a PDF file that is on my local computer in a way where anyone can retrieve the PDF?有什么方法可以上传我本地计算机上的 PDF 文件,任何人都可以检索 PDF?

If I do something like如果我做类似的事情

var filename = @"c:\test.pdf";
m.Attachments.Add(new Attachment(filename));

Anyone who goes onto the website has to have the same local path and same test.pdf file in the correct place (which would be impossible).任何进入网站的人都必须在正确的位置拥有相同的本地路径和相同的 test.pdf 文件(这是不可能的)。 So is there a way I can go about having to upload the pdf so it's not hard coded with a local path?那么有没有办法我可以 go 关于必须上传 pdf 所以它不是用本地路径硬编码的?

There are many ways to upload a file eg using an HTTP request and then download a file again using another HTTP request.有很多方法可以上传文件,例如使用 HTTP 请求,然后使用另一个 HTTP 请求再次下载文件。 If you just need to download files (no other API endpoints), consider using blob storage like AWS S3 bucket, Azure Blob Store or equivalents of other providers directly and configure the access rules in such a way that public read access is possible.如果您只需要下载文件(没有其他 API 端点),请考虑使用 AWS S3 存储桶、Azure Blob Store 或其他提供商的等效物等 blob 存储,并以可以公开读取访问的方式配置访问规则。 To download the file in your program you then just need an instance of HttpClient and make a request to your blob store.要在程序中下载文件,您只需要一个HttpClient实例并向您的 blob 存储发出请求。

If you want to upload files through ASP.NET have a look at the Microsoft docs which describe all things to be considered in detail.如果您想通过 ASP.NET 上传文件,请查看Microsoft 文档,其中详细描述了所有需要考虑的事项。 You will still need to store the file somewhere when using this approach.使用这种方法时,您仍然需要将文件存储在某处。 Either on disk of the server you run the application on or again a blob store (and there are of course other possibilites).无论是在您运行应用程序的服务器磁盘上还是在 blob 存储上(当然还有其他可能性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM