zoukankan      html  css  js  c++  java
  • webapi 文件下载输出接口

    public async ask<HttpResponseMessage> GetGuidingPrincipleDownload(string name ) 
    
            {
    
                bool status = false;
    
                try
    
                {
    
                    if (name != "")
    
                    {
    
                        name += name + ".pdf";
    
                        status = connectState(@"\10.10.10.178phiic_file_hivefdaBioEquiTestGuide", @"anonymous", "");//连接共享文件   connectState( 文件   用户名  密码)
    
                        if (status)
    
                        {
    
                            DirectoryInfo theFolder = new DirectoryInfo(@"\10.10.10.178phiic_file_hivefdaBioEquiTestGuide");
    
                            string path = Path.Combine(theFolder.ToString(), name);//路径
    
                            if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
    
                            {
    
                                string filename = Path.GetFileName(path);
    
                                var stream = new FileStream(path, FileMode.Open, FileAccess.Read);//打开文件
    
                                HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK)
    
                                {
    
                                    Content = new StreamContent(stream)
    
                                };
    
                                resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    
                                {
    
                                    FileName = filename
    
                                };
    
                                resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
                                resp.Content.Headers.ContentLength = stream.Length;
    
                                return await Task.FromResult(resp);
    
                            }
    
                        }
    
                    }
    
                }
    
                catch (Exception ex)
    
                {
    
                    Console.WriteLine(ex.Message);
    
                }
    
                return new HttpResponseMessage(HttpStatusCode.NoContent);
    
            }
    public static bool connectState(string path, string userName, string passWord)
    
            {
    
                bool Flag = false;
    
                Process proc = new Process();
    
                try
    
                {
    
                    proc.StartInfo.FileName = "cmd.exe";
    
                    proc.StartInfo.UseShellExecute = false;
    
                    proc.StartInfo.RedirectStandardInput = true;
    
                    proc.StartInfo.RedirectStandardOutput = true;
    
                    proc.StartInfo.RedirectStandardError = true;
    
                    proc.StartInfo.CreateNoWindow = true;
    
                    proc.Start();
    
                    //proc.StandardInput.WriteLine(@"Net Use {0} /del", path); //必须先删除,否则报错
    
                    string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
    
                    proc.StandardInput.WriteLine(dosLine);
    
                    proc.StandardInput.WriteLine("exit");
    
                    while (!proc.HasExited)
    
                    {
    
                        proc.WaitForExit(1000);
    
                    }
    
                    string errormsg = proc.StandardError.ReadToEnd();
    
                    proc.StandardError.Close();
    
                    if (string.IsNullOrEmpty(errormsg))
    
                    {
    
                        Flag = true;
    
                    }
    
                    else
    
                    {
    
                        throw new Exception(errormsg);
    
                    }
    
                }
    
                catch (Exception ex)
    
                {
    
                    throw ex;
    
                }
    
                finally
    
                {
    
                    proc.Close();
    
                    proc.Dispose();
    
                }
    
                return Flag;
    
            }
  • 相关阅读:
    query and join operation after sharding
    Windows Phone中的几种集合控件
    什么是SOPA SOPA的危害
    自动刷新人人网API session_key方法
    Windows Phone XNA创建简单局域网游戏
    static 修饰MySqlConnection引发的异常
    $Dsu$ $on$ $Tree$ 复习
    $Noip$前的小总结哦
    $NOIP2018$ 暴踩全场计划实施方案
    $NOIP2018$ 爆踩全场记
  • 原文地址:https://www.cnblogs.com/manwwx129/p/9553683.html
Copyright © 2011-2022 走看看