• 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;
    
            }
  • 相关阅读:
    第七周作业
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    第二周作业
    第一周作业
    老鼠与盈利
    币值转换
    2015 USP-ICMC gym 100733 J. Summer Wars
  • 原文地址:https://www.cnblogs.com/manwwx129/p/9553683.html
Copyright © 2020-2023  润新知