下载功能MVC 4

时间:2013-04-17 21:52:08

标签: asp.net-mvc download

我创建了一个web api,通过OAuth将用户连接到Dropbox。我正在使用API​​与Dropbox进行交互,Dropbox可以在我的本地工作,但是当我将API部署到我的Azure服务器时,我无法下载。我曾预料到这会发生,因为我的API目前很难编码到我的机器上的路径。 这是我正在使用的方法: 注意:我通过ActionResult调用此方法,作为项目MVC部分的一部分

public FileSystemInfo DownloadFile(string root, string path)
        {
            var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer),
                String.Format("files?root={0}&path={1}",
                root, UpperCaseUrlEncode(path)));

            var oauth = new OAuth();
            var requestUri = oauth.SignRequest(uri, _consumerKey, _consumerSecret, _accessToken);

            var request = (HttpWebRequest) WebRequest.Create(requestUri);
            request.Method = WebRequestMethods.Http.Get;
            var response = request.GetResponse();

            var metadata = response.Headers["x-dropbox-metadata"];
            var file = ParseJson<FileSystemInfo>(metadata);

            using (Stream responseStream = response.GetResponseStream())
            using (MemoryStream memoryStream = new MemoryStream())
            {
                byte[] buffer = new byte[1024];
                int bytesRead;
                do
                {
                    bytesRead = responseStream.Read(buffer, 0, buffer.Length);                    
                    memoryStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);

                file.Data = memoryStream.ToArray();
            }                                    
            return file;                                    
        }

这是我在动作结果中调用方法的地方。

var file = api.DownloadFile("dropbox", "Public/downloadThis.jpg");
                    path = file.Path;
                    file.Save(@"....\Desktop\DemoTest\Downloads\downloadThis.jpg"); --- this is the problem & *Save* is a stream writer

在浏览器上从服务器下载文件时是否需要遵循一个程序?

1 个答案:

答案 0 :(得分:0)

公共ActionResult下载(Models.downloadModel dowld,Models.LoggerView日志)         {

        string TC_ID = Request.QueryString["id"].ToString();
        string filename = TC_ID+"_LoggerData" + ".zip";
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
        Response.TransmitFile(Server.MapPath("~/files/" + filename));
        Response.End();

}