Ejemplo n.º 1
0
        private void StartAsyncTask(Object workItemState)
        {
            HttpRequest request = _context.Request;

            string fileID = request["id"];
            int    id     = 0;

            if (!String.IsNullOrEmpty(fileID) && Int32.TryParse(fileID, out id))
            {
                try
                {
                    if (id > 0)
                    {
                        FileVFS fileVFS = new FileVFS(Config.ConnectionString, id);
                        _context.Response.ContentType = fileVFS.Extension.MIMEType.Value;

                        string fileName = String.Format("inline;FileName=\"{0}.{1}\"", fileVFS.Name, fileVFS.Extension);
                        _context.Response.AddHeader("content-disposition", fileName);

                        _context.Response.BinaryWrite(fileVFS.FileContent);
                    }
                    _context.Response.Flush();
                }
                catch (Exception e)
                {
                    _context.Response.Write(e.Message);
                }
            }

            _completed = true;
            _callback(this);
        }
Ejemplo n.º 2
0
        private void StartAsyncTask(Object workItemState)
        {
            HttpRequest request = _context.Request;

            byte[] buffer        = new byte[request.ContentLength];
            string fullFileName  = Uri.UnescapeDataString(request.Headers["X-File-Name"]);
            string fileName      = Path.GetFileNameWithoutExtension(fullFileName);
            string fileExt       = Path.GetExtension(fullFileName);
            string documentIdStr = request["documentid"];
            int    fileId        = 0;
            Stream inputStream;

            if (String.IsNullOrEmpty(fullFileName) && request.Files.Count <= 0)
            {
                _context.Response.Write("{success:false}");
            }
            else
            {
                try {
                    if (fullFileName == null)
                    {
                        inputStream  = request.Files[0].InputStream;
                        fullFileName = Path.GetFileName(request.Files[0].FileName);
                        fileName     = Path.GetFileNameWithoutExtension(fullFileName);
                        fileExt      = Path.GetExtension(fullFileName);
                    }
                    else
                    {
                        inputStream = request.InputStream;
                    }

                    using (BinaryReader br = new BinaryReader(inputStream))
                        br.Read(buffer, 0, buffer.Length);

                    long hashCode = FileVFS.ComputeHash(buffer);

                    SqlConnection connection = new SqlConnection(Config.ConnectionString);
                    try {
                        connection.Open();
                        SqlTransaction trans = null;
                        try {
                            trans = connection.BeginTransaction();

                            bool existSameName = FileVFS.Exists(trans, fullFileName, 0);
                            if (existSameName)
                            {
                                fileId = FileVFS.GetFileID(trans, fullFileName, hashCode, 0);
                            }

                            if (fileId <= 0)
                            {
                                if (existSameName)
                                {
                                    fullFileName = fileName + "_" + hashCode + fileExt;
                                }

                                existSameName = FileVFS.Exists(trans, fullFileName, 0);
                                if (existSameName)
                                {
                                    fileId = FileVFS.GetFileID(trans, fullFileName, hashCode, 0);
                                }
                            }

                            if (fileId <= 0)
                            {
                                FileVFS fileVFS = new FileVFS(Config.ConnectionString, fullFileName, 0, buffer);
                                fileVFS.Create(trans);
                                fileId = fileVFS.ID;
                            }

                            if (fileId > 0 && !String.IsNullOrWhiteSpace(documentIdStr))
                            {
                                int documentID;
                                if (int.TryParse(documentIdStr, out documentID) && documentID > 0)
                                {
                                    DocumentFile df = new DocumentFile(Worker);
                                    df.FileID     = fileId;
                                    df.DocumentID = documentID;
                                    df.Insert(trans);
                                }
                            }

                            trans.Commit();
                        } catch (Exception) {
                            if (trans != null)
                            {
                                trans.Rollback();
                            }
                            throw;
                        }
                    } finally {
                        connection.Close();
                    }

                    _context.Response.Write(String.Format("{{ success: true, fileID: {0}, fileName: '{1}'}}", fileId, fullFileName));
                    _context.Response.Flush();
                } catch (Exception e) {
                    _context.Response.Write("{ success: false, error:\"" + e.Message + "\" }");
                }
            }

            _completed = true;
            _callback(this);
        }