public override void webExecute()
        {
            try
            {
                if (context.isMultipartRequest())
                {
                    localHttpContext.Response.ContentType = MediaTypesNames.TextPlain;
                    var r         = new List <UploadFile>();
                    var fileCount = localHttpContext.Request.GetFileCount();
                    for (var i = 0; i < fileCount; i++)
                    {
                        var      hpf      = localHttpContext.Request.GetFile(i);
                        string   fileName = string.Empty;
                        string[] files    = hpf.FileName.Split(new char[] { '\\' });
                        if (files.Length > 0)
                        {
                            fileName = files[files.Length - 1];
                        }
                        else
                        {
                            fileName = hpf.FileName;
                        }

                        string ext           = FileUtil.GetFileType(fileName);
                        string savedFileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), FileUtil.GetFileName(fileName), string.IsNullOrEmpty(ext) ? "tmp" : ext);
                        GxFile gxFile        = new GxFile(Preferences.getTMP_MEDIA_PATH(), savedFileName);

                        gxFile.Create(hpf.InputStream);

                        GXFileWatcher.Instance.AddTemporaryFile(gxFile);

                        r.Add(new UploadFile()
                        {
                            name         = fileName,
                            size         = gxFile.GetLength(),
                            url          = gxFile.GetPath(),
                            type         = context.GetContentType(ext),
                            extension    = ext,
                            thumbnailUrl = gxFile.GetPath(),
                            path         = savedFileName
                        });
                    }
                    UploadFilesResult result = new UploadFilesResult()
                    {
                        files = r
                    };
                    var jsonObj = JSONHelper.Serialize(result);
                    localHttpContext.Response.Write(jsonObj);
                }
                else
                {
                    Stream istream     = localHttpContext.Request.GetInputStream();
                    String contentType = localHttpContext.Request.ContentType;
                    String ext         = context.ExtensionForContentType(contentType);

                    string fileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), "BLOB", string.IsNullOrEmpty(ext) ? "tmp" : ext);
                    GxFile file     = new GxFile(Preferences.getTMP_MEDIA_PATH(), fileName);
                    file.Create(istream);

                    Jayrock.Json.JObject obj = new Jayrock.Json.JObject();
                    fileName = file.GetURI();

                    String fileGuid  = Guid.NewGuid().ToString("N");
                    String fileToken = GxRestPrefix.UPLOAD_PREFIX + fileGuid;
                    CacheAPI.FilesCache.Set(fileGuid, fileName, GxRestPrefix.UPLOAD_TIMEOUT);
                    obj.Put("object_id", fileToken);
                    localHttpContext.Response.AddHeader("GeneXus-Object-Id", fileToken);
                    localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson;
                    localHttpContext.Response.StatusCode  = 201;
                    localHttpContext.Response.Write(obj.ToString());
                }
            }
            catch (Exception e)
            {
                SendResponseStatus(500, e.Message);
                HttpHelper.SetResponseStatusAndJsonError(localHttpContext, HttpStatusCode.InternalServerError.ToString(), e.Message);
            }
            finally
            {
                try
                {
                    context.CloseConnections();
                }
                catch
                {
                }
            }
        }
예제 #2
0
        public override void webExecute()
        {
            try
            {
                string savedFileName, ext, fName;
                if (context.isMultipartRequest())
                {
                    localHttpContext.Response.ContentType = MediaTypesNames.TextPlain;
                    var r         = new List <UploadFile>();
                    var fileCount = localHttpContext.Request.GetFileCount();
                    for (var i = 0; i < fileCount; i++)
                    {
                        string fileGuid  = GxUploadHelper.GetUploadFileGuid();
                        string fileToken = GxUploadHelper.GetUploadFileId(fileGuid);
                        var    hpf       = localHttpContext.Request.GetFile(i);
                        fName = string.Empty;
                        string[] files = hpf.FileName.Split(new char[] { '\\' });
                        if (files.Length > 0)
                        {
                            fName = files[files.Length - 1];
                        }
                        else
                        {
                            fName = hpf.FileName;
                        }

                        ext           = FileUtil.GetFileType(fName);
                        savedFileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), FileUtil.GetFileName(fName), string.IsNullOrEmpty(ext) ? "tmp" : ext);
                        GxFile gxFile = new GxFile(Preferences.getTMP_MEDIA_PATH(), savedFileName);

                        gxFile.Create(hpf.InputStream);
                        string uri = gxFile.GetURI();
                        string url = (PathUtil.IsAbsoluteUrl(uri)) ? uri : context.PathToUrl(uri);

                        r.Add(new UploadFile()
                        {
                            name         = fName,
                            size         = gxFile.GetLength(),
                            url          = url,
                            type         = context.GetContentType(ext),
                            extension    = ext,
                            thumbnailUrl = url,
                            path         = fileToken
                        });
                        GxUploadHelper.CacheUploadFile(fileGuid, savedFileName, fName, ext, gxFile, localHttpContext);
                    }
                    UploadFilesResult result = new UploadFilesResult()
                    {
                        files = r
                    };
                    var jsonObj = JSONHelper.Serialize(result);
                    localHttpContext.Response.Write(jsonObj);
                }
                else
                {
                    Stream istream     = localHttpContext.Request.GetInputStream();
                    string contentType = localHttpContext.Request.ContentType;
                    WcfExecute(istream, contentType);
                }
            }
            catch (Exception e)
            {
                SendResponseStatus(500, e.Message);
                HttpHelper.SetResponseStatusAndJsonError(localHttpContext, HttpStatusCode.InternalServerError.ToString(), e.Message);
            }
            finally
            {
                try
                {
                    context.CloseConnections();
                }
                catch
                {
                }
            }
        }