webapi 文件下载输出接口

public async ask<HttpResponseMessage> GetGuidingPrincipleDownload(string name ) 

        {

            bool status = false;

            try

            {

                if (name != "")

                {

                    name += name + ".pdf";

                    status = connectState(@"\\10.10.10.178\phiic_file_hive\fdaBioEquiTestGuide", @"anonymous", "");//连接共享文件   connectState( 文件   用户名  密码)

                    if (status)

                    {

                        DirectoryInfo theFolder = new DirectoryInfo(@"\\10.10.10.178\phiic_file_hive\fdaBioEquiTestGuide\");

                        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;

        }

猜你喜欢

转载自www.cnblogs.com/manwwx129/p/9553683.html