private void dgvDocumenti_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                decimal   idDocumento = (decimal)dgvDocumenti.Rows[e.RowIndex].Cells[0].Value;
                Documenti d           = new Documenti();
                string    filename;
                byte[]    bytes     = d.EstraiDocumento(idDocumento, out filename);
                string    directory = @"c:\Preventivatore";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                string filePath = Path.Combine(directory, filename);

                FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();

                System.Diagnostics.Process.Start(filePath);
            }
            catch (Exception ex)
            {
                MostraEccezione("Errore in associa documento a prodotto finito", ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        public ActionResult EstraiDocumento(decimal IdDocumento)
        {
            Documenti d = new Documenti();
            string    filename;

            byte[] fileArray = d.EstraiDocumento(IdDocumento, out filename);
            if (fileArray.Length == 0)
            {
                return(null);
            }

            string contentType = string.Empty;

            switch (Path.GetExtension(filename).ToUpper())
            {
            case ".PDF":
                contentType = "application/pdf";
                break;

            case ".JPG":
            case ".JPEG":
            case ".JPE":
                contentType = "image/jpeg";
                break;

            case ".PNG":
                contentType = "image/png";
                break;

            case ".AVI":
                contentType = "video/avi";
                break;

            case ".PPS":
            case ".PPT":
                contentType = "application/mspowerpoint";
                break;

            case ".TIF":
            case ".TIFF":
                contentType = "image/tiff";
                break;

            case ".BMP":
                contentType = "image/bmp";
                break;

            case ".RTF":
                contentType = "application/rtf";
                break;

            case ".DOC":
            case ".DOT":
                contentType = "application/msword";
                break;

            case ".XLS":
            case ".XLSX":
                contentType = "application/excel";
                break;
            }

            Response.AppendHeader("content-disposition", string.Format(CultureInfo.InvariantCulture, "inline; filename={0}", filename));
            return(new FileStreamResult(new MemoryStream(fileArray), contentType));
        }