Multiform File Upload with Rest

I had seen a related question but no clear answers for my situation.   My fmx/android app is connected through the RestClient and I am trying to upload "any" file to a rest server that the docs specify with  these parameters.

*POST media/upload*
*This call allows a client to upload a new file. Contrary to all other calls, this is not a JSON call. Instead, it requires data to be posted with the content-type set to multipart/form-data and defines the following fields:*

Path      : Path to store new file at.
File      : The multipart file data.

Request 
 POST /media/upload
Path=Verdi/Aida/01 - Preludio.mp3
File=...

Here is one code iteration I tried but get "invalid parameter error" back from server

procedure TForm1.UpButtonClick(Sender: TObject);
var 
SendFile: TMemoryStream;
//FileStrm : TFileStream;  //not sure which stream method is best. 

begin


RESTRequest1.Resource := 'media/upload'; 
      RestRequest1.Method := rmPOST;
       RESTRequest1.Params.Clear;

try
SendFile := TMemoryStream.Create;
  SendFile.LoadFromFile(LItemPath);
RESTRequest1.Params.AddHeader('Content-Type', 'multipart/form-data');
RESTRequest1.Params.AddItem('PATH=','upload.mp4;','File=','+SendFile+;' , ctMULTIPART_FORM_DATA);
RESTRequest1.Execute;
finally
SendFile.Free;
end; 


This is all new to me,  Thanks For any help... 
Jay
0
Jay
2/3/2015 6:53:44 AM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

3 Replies
6099 Views

Similar Articles

[PageSpeed] 8
Get it on Google Play
Get it on Apple App Store

Jay wrote:

> RESTRequest1.Params.AddItem('PATH=','upload.mp4;','File=','+SendFile+;' 
, ctMULTIPART_FORM_DATA);

SendFile is a TMemoryStream.  You can't concatenate an object and a string 
together.

Try something more like this instead (untested):

{code}
procedure TForm1.UpButtonClick(Sender: TObject);
var 
  SendFile: TMemoryStream;
begin
  SendFile := TMemoryStream.Create;
  try
    SendFile.LoadFromFile(LItemPath);
    SendFile.Position := 0;

    RESTRequest1.Resource := 'media/upload'; 
    RESTRequest1.Method := rmPOST;
    RESTRequest1.Params.Clear;
    RESTRequest1.Params.AddItem('PATH', 'upload.mp4', pkGETorPOST, [], ctMULTIPART_FORM_DATA);
    RESTRequest1.AddBody(SendFile, ctMULTIPART_FORM_DATA);

    RESTRequest1.Execute;
  finally
    SendFile.Free;
  end; 
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
2/3/2015 8:26:23 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Jay wrote:
> 
> > RESTRequest1.Params.AddItem('PATH=','upload.mp4;','File=','+SendFile+;' 
> , ctMULTIPART_FORM_DATA);
> 
> SendFile is a TMemoryStream.  You can't concatenate an object and a string 
> together.
> 
> Try something more like this instead (untested):
> 
> {code}
> procedure TForm1.UpButtonClick(Sender: TObject);
> var 
>   SendFile: TMemoryStream;
> begin
>   SendFile := TMemoryStream.Create;
>   try
>     SendFile.LoadFromFile(LItemPath);
>     SendFile.Position := 0;
> 
>     RESTRequest1.Resource := 'media/upload'; 
>     RESTRequest1.Method := rmPOST;
>     RESTRequest1.Params.Clear;
>     RESTRequest1.Params.AddItem('PATH', 'upload.mp4', pkGETorPOST, [], ctMULTIPART_FORM_DATA);
>     RESTRequest1.AddBody(SendFile, ctMULTIPART_FORM_DATA);
> 
>     RESTRequest1.Execute;
>   finally
>     SendFile.Free;
>   end; 
> end;
> {code}
> 
> -- 
> Remy Lebeau (TeamB)

Thanks Remy,
Unfortunately it didnt like that either.   actual server error I'm getting is "invalid file parameter".  But I dont know if thats a catch all message covering all param errors and cant get in touch with server programmer.  
I ran process with debug and message client shoots out is "exception class EIdHTTPProtocolException http/1.0 400 bad request"

I have google pilot and or Rest Debugger and would test it that way, but not sure how to formulate a request in it with actual file involved. 

Jay
0
Jay
2/4/2015 1:37:43 PM
Jay wrote:

> Unfortunately it didnt like that either.   actual server error I'm
> getting is "invalid file parameter".  But I dont know if thats a catch
> all message covering all param errors and cant get in touch with
> server programmer.
> I ran process with debug and message client shoots out is "exception
> class EIdHTTPProtocolException http/1.0 400 bad request"
> I have google pilot and or Rest Debugger and would test it that way,
> but not sure how to formulate a request in it with actual file
> involved.

I have never used the REST components, so I cannot help you further with 
them.  All I can tell you is that you have to use AddBody() in order to send 
a TStream, and you need the ctMULTIPART_FORM_DATA content type.  But I don't 
know the correct syntax to specify additional parameters.  Since the REST 
components use Indy internally, you are probably better off just using Indy's 
TIdHTTP and TIdMultipartFormDataStream components directly, eg:

{code}
procedure TForm1.UpButtonClick(Sender: TObject);
var 
  MultiPartStrm: TIdMutipartFormDataStream;
begin
  MultiPartStrm := TIdMultipartFormDataStream.Create;
  try
    MultiPartStrm.AddFormField('PATH', 'upload.mp4');
    MultiPartStrm.AddFile('File', LItemPath);
    IdHTTP1.Post('http://hostname/media/upload', MultiPartStrm); 
  finally
    MultiPartStrm.Free;
  end;
end; 
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
2/4/2015 6:55:37 PM
Reply:

Similar Artilces:

file uploader save file on server and in database and upload this file on client
hi,i have a file uploader control user can attach files (.rar, .zip, etc) and store file folder and path in database. and another user can click of attach link and can download that file. how can i do this please solve out this problem. you can find your problem solution in this link and you can store the file name in your database server.http://www.aspnettutorials.com/tutorials/network/net-fileupload-aspnet2-csharp.aspxPlease Mark as Answred If This Blog has Helped You.RegardsMitesh Darjihttp://www.indianic.comhttp://www.mitatdotnet.blogspot.com As always there are many ways and which ...

File Upload
Hi,  I am using the file upload code at the link below to allows users to upgrade multiple files into  a storage area. http://dotnetslackers.com/community/blogs/basharkokash/archive/2007/09/11/Test.aspx My question is how do I identify which files belong to which user?? On my site i am using ASP membership and before uploading files users must logg on. Does this mean I can use the UserID to identify them or is there another simple wayI I have the file upload in a simple wizard form. Also i am coding in C sharp   Thank you Amereto   You can use the logged...

Deleting A File Uploaded Through File Upload Control
Hello, I have a form which allows a user to upload a file to a virtual drive on our server using the following code: fileInvoice.SaveAs(ConfigurationManager.AppSettings.Get("DocumentPath") + strFileName); //Document Path is defined in the web.config file How can I safely allow a form on a web page to delete the file if they know the exact name of the file. I don't want them to be able to do a wildcard match like test* thanks in advance, mitch  Peace be on you brother, so you want to safely delete the file which the name you know. OK I consider that the name ...

File Upload
Hey I've got a fileUpload control on an aspx page that works fine for the accepted file type, .rpts, and brings up the correct error message for not allowed extensions such as .exes and .jpgs. But .pdf files should cause an error message to be displayed, as only .rpt files are allowed to be uploaded, but instead it brings up a blank page like a web500 page. Is there a known bug when attempting to upload pdf's?   Just in case,here's the only relevant code attached to the control:   If uploadFilename.EndsWith(".rpt") = True Then 'ensures only .rp...

Upload all files in folder with file upload control
H i I'm trying to upload all files to a wesite that is in a specific local folder. My code workes fine when I run it locally but when I upload it it fails. I think it looks for the files to upload on the web server and thus cannot find it.  1 Dim files() As String 2 folderExists = System.IO.Directory.Exists(foldername) 3 If folderExists = False Then 4 Label1.Text = "Folder does not exist. Did you enter a folder name or just a file name ?" 5 Exit sub 6 End If 7 files = System.IO.Directory.GetFiles(foldername) 8 For...

file upload extensions
Is it possible to upload any/some video files? I tried to add file extentions under the host settings but it didn't work. Wonder if if it's possible in dnn to upload any video files?Thanks. It is possible to upload files of any extension (as far as I know).  You might be hitting a file size constraint?  I've never tried uploading any really huge files - maybe someone else with comment about that?-doug, Powerhouse Data Yes, you can upload video files.  I've uploaded them and used with core modules as well as MediaGallery.  By default, DNN has a max upload size of 8MB...

How to identify the original file type and restrict the file uploading while using file upload control
 Hi asp.net , I am uploading a file using FileUpload control in asp.net using vb.net.I wrote the code to upload only PDF files. the user can change the extension and change to PDF . I have to check is the file original PDF or not then i have to upload the PDF files only . How to do this ??  Hi, Check the file Mime type for "application/pdf" by looking at the:  FileUpload1.PostedFile.ContentType RegardsSmcoxonNo Gem is ever polished without some friction. Hi smcoxon , Thanks For Replying ,But i found that there is no use using PostedFil...

File Upload- Uploads a blank 90kb file
Hi, I'm uploading a 90kb file to both my oracle database and the internet server from asp.net 2.0 via fileupload tag.The file uploads correctly into oracle but uploads with the correct filename but with 0 bytes into my server. Has anyone come across this issue? My Code for saving into my server: Me.filMyFile.SaveAs(Server.MapPath("~/" & "zQ2_" & datever & _ Me.filMyFile.FileName.ToString()))         Please help me solve this issue.Thank YouVic Is the file being created with zero byte?ASP.NET Hosting (HostingFest) Yes, the file uplo...

Upload file from remote Computer using File Upload
Hi,     How to upload file from remote computer. Can i browse it thruough the File Upload control ?    Please help?     RamarjunK:Hi,     How to upload file from remote computer. Can i browse it thruough the File Upload control ?    Please help?  you can. when the file open dialog box comes, choose my network places, select the corrosponging share, specify the credentials.  then choose the file.you haven't do any thing special, coding is same .Give a man a fish and you feed him for a day. Teach a man to fish a...

After Uploading File, Refreshing Page causes file to upload again
As my subject says, after using the FileUpload control to send a file to my server, if the user presses F5 to refresh the page, the file will be uploaded again. The upload control does not list a file, but the tracing on the page does list items under Content_Length and Content_Type under Headers Collection and Server Variables. CONTENT_LENGTH570032CONTENT_TYPEmultipart/form-data; boundary=---------------------------7d6119a16138cI tried clearing these out by using the following commands after my initial file upload without success:Response.ContentType = null;Response.ClearContent();Resp...

File Upload
I use 2.0 with VS 2008 In my code I have put a file upload limit of 1 MB. But if I try uploading files greater than 4 MB, instead of showing the User Message, it does not execute any line of code. Instead it shows. Internet Explorer cannot display the webpage But if I try uploading files between 1MB+ and -4MB, it shows the user message correctly. Cant seem to figure out what the problem could be. Any hints? Below is my codeDim path As String = Server.MapPath("~/UploadedFiles/") Dim fileOK As Boolean = False If FileUpload1.HasFile Then If FileUpload1.PostedFi...

difference between asp file upload and html file upload
Can any tell me the the difference between asp.net file upload control and html file upload control..? is there any filesize & file type limitation for both of these? or can we upload any file of any size? Thanks As far as I know, thery are most likely the same in terms of functionality..http://msdn.microsoft.com/en-us/library/ms227677(VS.80).aspxhttp://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlinputfile(VS.80).aspx Regards,Vinz"Code, Beer and Music" that's my way of being a programmer!How to get your Forum Question Answered | Blog | CodeASP.NE...

Processes file content before upload (from file upload form)
Hello I have a web aplication that needs to process file content (client side) before uploading them to server. May be I have to use file upload form and somehow read files and sign them on submit event I'm not sure how (and can I) do this in Firefox extension? I prefer to use browser functionality for file upload rather than make my own HTTP connection from my extension. I also prefer not to write any user interface. On 4/7/10 7:27 AM, Юлиян wrote: > I have a web aplication that needs to process file content (client > side) before uploading them to server. htt...

upload file by file
hi all,   we have project, local in server, more then one people do the update, let's say, if all file local in test, I did the modify in local and build and upload the whole test file into service, it work fine. but if I only upload helloone.aspx. and its Vb file (under test) into service, it did not work( the VB code) , is that a way to only upload file by file to service, thank you   If I understand your problem correctly, I think the problem that you may be having is that after pages are compiled locally they are not being compiled for the entire site.  I would su...

Web resources about - Multiform File Upload with Rest - embarcadero.delphi.firemonkey

The Eleventh Hour (Doctor Who) - Wikipedia, the free encyclopedia
The newly regenerated Doctor confronts one of the Atraxi. This scene was described as where the new incarnation, played by Matt Smith, "finally ...

Biography - Sir John Robertson - Australian Dictionary of Biography
Sir John Robertson (1816-1891), land reformer and politician, was born on 15 October 1816 at Bow near London, third son and fourth child of James ...

Jane Addams: Newer Ideals of Peace: Chapter 1: Introduction
The following pages present the claims of the newer, more aggressive ideals of peace, as over against the older dovelike ideal. These newer ideals ...

Collection of JG Ballard critical articles
JG Ballard Criticism & Analysis 1973: Krafft-Ebing Visits Dealey Plaza Jerome Tarshis reviews The Atrocity Exhibition and interviews JGB about ...

Genesis 11:7 Come, let us go down and confuse their language so they will not understand each other." ...
New International Version (©1984) Come, let us go down and confuse their language so they will not understand each other." New Living Translation ...

Pandora Looks To Both Local And International For Ad Growth
Internet radio player Pandora has had some ups and downs since going public a year ago. For example, in the past month, streaming music service ...

Multiform Thoroughbred
Pedigree for Multiform, photos and offspring from the All Breed Horse Pedigree Database.

Culture and Anarchy - Chapter IV (by Matthew Arnold)
The complete text of Culture and Anarchy

1. Renascence. Millay, Edna St. Vincent. 1917. Renascence and Other Poems
1. Renascence. Millay, Edna St. Vincent. 1917. Renascence and Other Poems

Viewer Discretion
BOTH FLESH AND NOT is David Foster Wallace at his best and his worst, but the thing about Wallace’s best was that it usually contained his worst: ...

Resources last updated: 12/11/2015 10:34:34 AM