I am using Restsharp to upload files. I need to show progress bar, but got to know that they don't support uploading in chunks. Below is the portion of code which uploads, how to modify it to enable uploading in chunks? Its in file Http.Synch.cs
private static HttpWebResponse GetRawResponse(HttpWebRequest request)
{
try
{
return (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
if (ex.Response is HttpWebResponse)
{
return ex.Response as HttpWebResponse;
}
throw;
}
}
Thanks
Sujit
Related
I have an API that returns a large number of data in a list.
when I am calling the API from postman I can't see the response.
i seen some solutions on google but in that they are setting
maxContentLength in web.config file.
but i haven't web.config file i have project in .net core (5.0)
There is anything that I need to do to increase API response?
here in this screenshot while debugging I can see the response.
here is the code :
[HttpPost]
[Route("ReconcileUpload")]
[Consumes("multipart/form-data")]
[RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
[RequestSizeLimit(52428800)]
[hasAccess("OPRT_TXN")]
public ReconcileResponse ReconcileUpload([FromForm] IFormFile file, [FromQuery] BankChallanType FileType)
{
ReconcileResponse response = new ReconcileResponse();
string storePath = "";
using (var session = db.Database.BeginTransaction())
{
try
{
//response filling
}
catch (Exception e)
{
session.Rollback();
Logger.Error($"Error in {MethodBase.GetCurrentMethod().Name} {e.Message} at {e.StackTrace}");
throw;
}
finally
{
if (!string.IsNullOrEmpty(storePath))
System.IO.File.Delete(storePath);
}
var x= response;
return x;
}
}
I'm trying to save pdf in wildfly, I'm using RestEasy MultipartFormDataInput provided with wildfly 20.0.1,
but it doesn't work.
This is what I have:
public static Response uploadPdfFile(MultipartFormDataInput multipartFormDataInput) {
// local variables
MultivaluedMap<String, String> multivaluedMap = null;
String fileName = null;
InputStream inputStream = null;
String uploadFilePath = null;
try {
Map<String, List<InputPart>> map = multipartFormDataInput.getFormDataMap();
List<InputPart> lstInputPart = map.get("poc");
for(InputPart inputPart : lstInputPart){
// get filename to be uploaded
multivaluedMap = inputPart.getHeaders();
fileName = getFileName(multivaluedMap);
if(null != fileName && !"".equalsIgnoreCase(fileName)){
try {
// write & upload file to UPLOAD_FILE_SERVER
//here I have the error: Unable to find a MessageBodyReader for media type:
//application/pdf
inputStream = inputPart.getBody(InputStream.class,InputStream.class);
uploadFilePath = writeToFileServer(inputStream, fileName);
}catch (Exception e) {
e.printStackTrace();
}
// close the stream
inputStream.close();
}
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
// release resources, if any
}
return Response.ok("File uploaded successfully at " + uploadFilePath).build();
}
I'm using postman for test, http POST method, in the body I send: form-data - file and selected the file.pdf.
When I sent the request, I have the next RunTimeException when I try:
inputStream = inputPart.getBody(InputStream.class,null);
I get:
java.lang.RuntimeException: RESTEASY007545: Unable to find a MessageBodyReader for media type: application/pdf and class type org.jboss.resteasy.util.Base64$InputStream
At the moment I am saving the file receiving it in Base64, but I think that with MultipartFormDataInput it is the correct way.
This is what I have when debug:
Thanks for your support.
I solved this changing the InputStream from "org.jboss.resteasy.util.Base64.InputStream"
to "java.io.InputStream"
This method connect given path which has to be xml file and save xml file to somewhere in server. Finally, download xml file from server by calling _downloadFileFromServer method
public async Task SaveFiletoServer(int id, string nud_preparationValue, int nud_divideNumber, string saveConfigPath, string saveConfigName)
{
try
{
await tryToConnect(id, nud_preparationValue, nud_divideNumber);
ReadXml.Save(XDocument,saveConfigPath,saveConfigName, nud_divideNumber);
await _downloadFileFromServer(saveConfigPath, saveConfigName);
}
catch (System.Exception ex)
{
ViewBag.DownloadError = ex.Message;
}
}
this method is using by above method(Download xml file from server).
private async Task _downloadFileFromServer(string saveConfigPath, string saveConfigName)
{
var memory = new MemoryStream();
using (var stream = new FileStream(string.Concat(saveConfigPath,saveConfigName,".xml"), FileMode.Open))
{
stream.CopyTo(memory);
}
memory.Position = 0;
await Task.FromResult(File(memory, "application/xml",string.Concat(saveConfigName,".xml")));
}
If I directly call _downloadFileFromServer method, it works correctly.xml file is downloading. However, when I call SaveFiletoServer method, _downloadFileFromServer method doesn't work correctly. Xml file is not downloading. I don't understand what is wrong.
Problem solved by using html.actionlink instead of ajax in order to download xml file with SaveFiletoServer method
I have to upload and download a file using FTP server but I am facing issues in it. Have gone though many solutions but nothing seems to be working.
I am using secureftp-test.com as the testing FTP server.
Below is the code for uploading where in I am using FTPClient storeFile method but it doesn't seems to work.
public static void main(String[] args) {
String server = "ftp.secureftp-test.com";
int port = 21;
String user = "test";
String pass = "test";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
boolean login = ftpClient.login(user, pass);
System.out.println("login " + login);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File firstLocalFile = new File("D:/jetty.pdf");
String firstRemoteFile = "myfile.pdf";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("The first file is uploaded successfully.");
} else {
System.out.println("upload failed");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Each time the output is "upload failed". I don't understand where i am wrong.
Your port is incorrect. It uses:
FTPS via Auth TLS/SSL and implicit FTP over SSL on port 990
Also, if you read the site carefully, upload is forbidden:
Chilkat provides this FTPS test account for anyone wishing to test secure FTP client functionality. You may connect to ftp.secureftp-test.com, login as "test" with password "test", and download any files present on the server. The "test" account may also retrieve directory listings. However, it is restricted from uploading files to the server.
Reference: secureftp-test.com
I have an RPC service and one of the method is generating a report using Pentaho Reporting Engine. Report is an PDF file. What I'd like to do, is when user request a report, the report is sent back to him and save dialog or sth pops up. I tried this inside my service method:
Resource res = manager.createDirectly(new URL(reportUrl), MasterReport.class);
MasterReport report = (MasterReport) res.getResource();
report.getParameterValues().put("journalName", "FooBar");
this.getThreadLocalResponse().setContentType("application/pdf");
PdfReportUtil.createPDF(report, this.getThreadLocalResponse().getOutputStream());
But it doesn't work. How it can be done?
I do it a little bit differently. I've got a separate servlet that I use to generate the PDF. On the client, do something like:
Cookies.setCookie(set what ever stuff PDF needs...);
Window.open(GWT.getModuleBaseURL() + "DownloadPDF", "", "");
The servlet, DownloadPDF looks something like this:
public class DownloadPDF extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
try {
// get cookies, generate PDF.
// If PDF is generated to to temp file, read it
byte[] bytes = getFile(name);
sendPDF(response, bytes, name);
} catch (Exception ex) {
// do something here
}
}
byte[] getFile(String filename) {
byte[] bytes = null;
try {
java.io.File file = new java.io.File(filename);
FileInputStream fis = new FileInputStream(file);
bytes = new byte[(int) file.length()];
fis.read(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
void sendPDF(HttpServletResponse response, byte[] bytes, String name) throws IOException {
ServletOutputStream stream = null;
stream = response.getOutputStream();
response.setContentType("application/pdf");
response.addHeader("Content-Type", "application/pdf");
response.addHeader("Content-Disposition", "inline; filename=" + name);
response.setContentLength((int) bytes.length);
stream.write(bytes);
stream.close();
}
}