public static bool GetHttpRequestPostedFile(HttpContext httpContext, string varName, out string filePath) { filePath = null; if (httpContext != null) { var pf = GetFormFile(httpContext, varName); if (pf != null) { #pragma warning disable SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}' FileInfo fi = new FileInfo(pf.FileName); #pragma warning restore SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}' string tempDir = Preferences.getTMP_MEDIA_PATH(); string ext = fi.Extension; if (ext != null) { ext = ext.TrimStart('.'); } filePath = FileUtil.getTempFileName(tempDir, "BLOB", ext); GXLogging.Debug(log, "cgiGet(" + varName + "), fileName:" + filePath); GxFile file = new GxFile(tempDir, filePath); #if NETCORE filePath = file.Create(pf.OpenReadStream()); #else filePath = file.Create(pf.InputStream); #endif GXFileWatcher.Instance.AddTemporaryFile(file); return(true); } } return(false); }
internal void WcfExecute(Stream istream, string contentType) { string savedFileName, ext, fName; ext = context.ExtensionForContentType(contentType); savedFileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), "BLOB", string.IsNullOrEmpty(ext) ? "tmp" : ext); GxFile file = new GxFile(Preferences.getTMP_MEDIA_PATH(), savedFileName); file.Create(istream); JObject obj = new JObject(); fName = file.GetURI(); string fileGuid = GxUploadHelper.GetUploadFileGuid(); string fileToken = GxUploadHelper.GetUploadFileId(fileGuid); obj.Put("object_id", fileToken); localHttpContext.Response.AddHeader("GeneXus-Object-Id", fileGuid); localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson; HttpHelper.SetResponseStatus(localHttpContext, ((int)HttpStatusCode.Created).ToString(), string.Empty); localHttpContext.Response.Write(obj.ToString()); GxUploadHelper.CacheUploadFile(fileGuid, savedFileName, fName, ext, file, localHttpContext); }
public short Save() { try { if (IsReadOnly()) { errCod = 13; errDescription = "Can not modify a readonly document"; p.Dispose(); } else { using (var stream = new MemoryStream()) { p.SaveAs(stream); p.Dispose(); GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private); stream.Position = 0; file.Create(stream); } } } catch (Exception e) { GXLogging.Error(log, "Error saving file " + xlsFileName, e); errCod = 12; errDescription = "Could not save file." + e.Message; return(-1); } return(0); }
public short Save() { try { GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private); MemoryStream content = new MemoryStream(); GxExcelUtils.Invoke(ef, "SaveXls", new object[] { content }); content.Position = 0; file.Create(content); } catch (Exception e) { GXLogging.Error(log, "Error saving " + xlsFileName, e); errCod = 12; errDescription = "Could not save file." + e.Message; return(-1); } return(0); }
private string getBLOBFile(int id, string extension, string name, string fileName, bool temporary, GxFileType fileType = GxFileType.PrivateAttribute) { GxFile file = null; Stream fs = null; BinaryWriter bw = null; int bufferSize = 4096; byte[] outbyte = new byte[bufferSize]; long retval; long startIndex = 0; bool streamClosed = false; try { startIndex = 0; retval = _gxDbCommand.Db.GetBytes(_gxDbCommand, _DR, id - 1, startIndex, outbyte, 0, bufferSize); if (retval == 0) { return(""); } using (fs = new MemoryStream()) { using (bw = new BinaryWriter(fs)) { while (retval == bufferSize) { bw.Write(outbyte); bw.Flush(); startIndex += bufferSize; retval = _gxDbCommand.Db.GetBytes(_gxDbCommand, _DR, id - 1, startIndex, outbyte, 0, bufferSize); } bw.Write(outbyte, 0, (int)retval); bw.Flush(); fs.Seek(0, SeekOrigin.Begin); file = new GxFile(_gxDbCommand.Conn.BlobPath, fileName, fileType); file.Create(fs); } } streamClosed = true; GXLogging.Debug(log, "GetBlobFile fileName:" + fileName + ", retval bytes:" + retval); if (temporary) { GXFileWatcher.Instance.AddTemporaryFile(file); } fileName = file.GetURI(); } catch (IOException e) { if (!file.Exists()) { GXLogging.Error(log, "Return getBLOBFile Error Can't read BLOB field into " + fileName, e); throw (new GxADODataException(e)); } else { GXLogging.Warn(log, "Return getBLOBFile Error Can't write BLOB field into " + fileName, e); } } finally { if (!streamClosed) { try { if (bw != null) { bw.Close(); } if (fs != null) { fs.Close(); } } catch (Exception ex) { GXLogging.Error(log, "getBLOBFile Close Stream Error", ex); } } } return(fileName); }
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 { } } }
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 { } } }