简体   繁体   中英

Access files from another project same solution asp.net mvc

I have solution "solution" and two projects:

  • (here user uploads file to some folder like "~/uploads" (此处用户将文件上传到“〜/ uploads”之类的文件夹中
  • (here I must access user files) (在这里我必须访问用户文件)

In web api project I access files like this:

    public HttpResponseMessage GetPdfPage()
    {
        HttpResponseMessage responce =  new HttpResponseMessage();
        responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath("~/somefile.pdf"), FileMode.Open, FileAccess.Read));
        responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return responce;
    }

How must I modify path to file?

我认为共享文件夹是一个更好的解决方案http://support.microsoft.com/kb/324267

I also faced a similar kind of problem where I need to access the Upload folder of another project(main) from the BLL project under same solution. For that I have used absolute path(not hardcoded) for the Upload folder. I think the code below should also work for you.

public HttpResponseMessage GetPdfPage()
    {
        HttpResponseMessage responce =  new HttpResponseMessage();
        string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath(basePath +"somefile.pdf"), FileMode.Open, FileAccess.Read));
        responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return responce;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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